我的代码如下:错误出现在try里面的mAppWidgetManager.bindAppWidgetId(appWidgetId, xmicIntent.getComponent()); 错误为: ERROR/AndroidRuntime(335): Caused by: java.lang.reflect.InvocationTargetException
请教各位高人,错的原因是什么呢?要如何改呢
Intent xmicIntent = new Intent(Intent.ACTION_MAIN, null);
xmicIntent.addCategory(Intent.CATEGORY_DEFAULT);
mAppWidgetManager = AppWidgetManager.getInstance(this);
List installed = mAppWidgetManager.getInstalledProviders();
String packageName = installed.get(0).provider.getPackageName();
String className = installed.get(0).provider.getClassName();
intent.setClassName(packageName, className);
try {
mAppWidgetManager.bindAppWidgetId(appWidgetId, xmicIntent.getComponent());
} catch (IllegalArgumentException e) {
Log.v("TEST", "hello-----------------------------------------------------");
}
addAppWidget(xmicIntent);
[code="java"]
你试试这样吧··
appWidgetId----Widget的id 这个应该取的到吧
Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
onActivityResult(Intent intent){
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
mAppWidgetManager.bindAppWidgetId(mAppWidgetId, intent.getComponent());
}
[/code]
为啥不在AndroidManifest.xml配置使用widget
看下你的Class是怎么定义的?
:o 你想直接添加一个Widget 到桌面???
[code="java"]
public void bindAppWidgetId (int appWidgetId, ComponentName provider)
Since: API Level 3
Set the component for a given appWidgetId.
You need the APPWIDGET_LIST permission. This method is to be used by the AppWidget picker.
Parameters
appWidgetId The AppWidget instance for which to set the RemoteViews.
provider The BroadcastReceiver that will be the AppWidget provider for this AppWidget.
冒失这个还需要app与系统一致的签名啊··
[/code]
直接修改源码???
直接在launcher源码中修改?
2个链接
第一lanuncher添加widget 流程·
[url]http://blog.csdn.net/hmg25/article/details/6439414[/url]
第二 android源码单步跟踪
[url]http://wenku.baidu.com/view/8e4927f5ba0d4a7302763aad.html[/url]
8) 你有源码
看看这个方法
[code="java"] onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); [/code]
8) 那这个就得比较复杂了··
你得先读完launcher源码··
肯定是可以自己定制的