从AppWidget开始MainActivity不起作用 [英] Start MainActivity from AppWidget doesn't work
问题描述
我想从我的AppWidget启动我MainActivity与挂起的意图,但我想不出为什么它不火的意图。该插件具有最多3个项目,这是结合于RemoteViewsService一个ListView。我要的是点击任何行的时候开始我MainActivity。谁能告诉我我可能做错了?
我已经查了一些相关的帖子这样启动活动从AppWidget ,甚至试图从这里 https://github.com/运行示例commonsguy / CW-advandroid /树/主/ AppWidget ,虽然样本的作品,仍然无法向我做了什么不同的。
下面是我的一些code的是相关的,请让我知道我是否应该提前发布更多,并且道歉,如果我的帖子不看的权利,因为这是我的第一个问题:
控件布局:
<?XML版本=1.0编码=UTF-8&GT?;
< ListView控件的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /股
机器人:layout_width =match_parent
机器人:layout_height =match_parent
/>
AppWidgetProvider类:
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
Log.d(LOGTAG的onUpdate);
Log.d(LOGTAGappWidgetIds.length =+ appWidgetIds.length);
的for(int i = 0; I< appWidgetIds.length ++我){
Log.d(LOGTAG,appWidgetIds [I] =+ appWidgetIds [I]);
}
//使用远程适配器更新每个应用程序的窗口小部件
的for(int i = 0; I< appWidgetIds.length ++我){
updateWidget(上下文,appWidgetManager,appWidgetIds [I]);
}
super.onUpdate(背景下,appWidgetManager,appWidgetIds);
} 公共无效updateWidget(上下文的背景下,AppWidgetManager appWidgetManager,诠释appWidgetId){
Log.d(LOGTAGupdateWidget ID =+ appWidgetId); //设置向上指向StackViewService即会意图
//为这个集合的意见。
意向意图=新意图(背景下,StockAppWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
//当意图进行比较时,演员会被忽略,所以我们需要嵌入演员
//为使临时演员不会被忽略的数据。
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews RV =新的RemoteViews(context.getPackageName(),R.layout.stock_appwidget);
rv.setRemoteAdapter(R.id.stocks,意向); 意图contentIntent =新意图(背景下,MainActivity.class);
//contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
的PendingIntent startAppPendingIntent =
PendingIntent.getActivity(上下文,0,contentIntent,0);
rv.setPendingIntentTemplate(R.id.stocks,startAppPendingIntent); appWidgetManager.updateAppWidget(appWidgetId,RV);
}
清单:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=x40241.chris.yuan.a4.app
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =14
机器人:targetSdkVersion =16/> <使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/> <应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@安卓风格/ Theme.Holo.Light.DarkActionBar>
<活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=。SearchableActivity>
&所述;意图滤光器>
<作用机器人:名字=x40241.chris.yuan.a4.app.SEARCH/>
<类机器人:名字=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
< /活性GT;
<服务机器人:名字=。StockServiceImpl/>
<服务机器人:名字=。StockAppWidgetService
机器人:权限=android.permission.BIND_REMOTEVIEWS/>
<接收机器人:名字=。ServiceLauncher>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /接收器>
<接收机器人:名字=。StockAppWidgetProvider>
&所述;意图滤光器>
<作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>
&所述; /意图滤光器>
<元数据机器人:名字=android.appwidget.provider
机器人:资源=@ XML / stock_appwidget_info/>
< /接收器>
< /用途>< /清单>
我想我找到了答案,或在相关岗位至少一个可行的解决方案:
<一个href=\"http://stackoverflow.com/questions/12958186/adapterviewflipper-in-app-widget-setpendingintenttemplate-and-setonclickfilli\">AdapterViewFlipper在应用小工具:setPendingIntentTemplate()和setOnClickFillInIntent()不工作
原来我没有被正确设定FillInIntent。我应该调用setOnClickFillInIntent上列表项,而不是列表本身的顶层视图。这里的code修改,使得它在RemoteViewsFactory工作:
公共RemoteViews getViewAt(INT位置){
如果(调试)Log.d(LOGTAG,StockRemoteViewsFactory getViewAt位置=+位置); RemoteViews RV =新的RemoteViews(mContext.getPackageName(),R.layout.widget_item);
rv.setTextViewText(R.id.text_symbol,mStockItems.get(位置).getSymbol());
rv.setTextViewText(R.id.text_price,的String.format($%2f上。,mStockItems.get(位置).getPrice())); 意图fillInIntent =新意图(Intent.ACTION_MAIN);
fillInIntent.putExtra(StockAppWidgetProvider.ITEM_NUM,位置); //设置列表项的顶层视图,而不是列表视图本身
//rv.setOnClickFillInIntent(R.id.stocks,fillInIntent);
rv.setOnClickFillInIntent(R.id.general_layout,fillInIntent); 返回rv;
}
I'm trying to launch my MainActivity from my AppWidget with pending intent but I can't figure out why it doesn't fire the intent. The widget has a ListView with up to 3 items, which is bind to a RemoteViewsService. I want to start my MainActivity when any of the rows is clicked. Can anyone tell me what I might done wrong?
I already checked some related posts like this Launching an activity from an AppWidget, and even tried to run the sample from here https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget, and while the sample works, still can't tell what I did differently.
Here are some of my code that are relevant, please let me know if I should post more, and apologize in advance if my post doesn't look right as this is my first question:
Widget layout:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/stocks"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
AppWidgetProvider class:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(LOGTAG, "onUpdate");
Log.d(LOGTAG, "appWidgetIds.length="+appWidgetIds.length);
for (int i = 0; i < appWidgetIds.length; ++i) {
Log.d(LOGTAG, "appWidgetIds[i]="+appWidgetIds[i]);
}
// update each of the app widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
updateWidget(context, appWidgetManager, appWidgetIds[i]);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d(LOGTAG, "updateWidget id="+appWidgetId);
// Sets up the intent that points to the StackViewService that will
// provide the views for this collection.
Intent intent = new Intent(context, StockAppWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.stock_appwidget);
rv.setRemoteAdapter(R.id.stocks, intent);
Intent contentIntent = new Intent(context, MainActivity.class);
//contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent startAppPendingIntent =
PendingIntent.getActivity(context, 0, contentIntent, 0);
rv.setPendingIntentTemplate(R.id.stocks, startAppPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x40241.chris.yuan.a4.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="x40241.chris.yuan.a4.app.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".StockServiceImpl" />
<service android:name=".StockAppWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<receiver android:name=".ServiceLauncher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".StockAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/stock_appwidget_info" />
</receiver>
</application>
</manifest>
I think I found the answer, or at least a workable solution in a related post:
It turns out I wasn't setting the FillInIntent properly. I should call setOnClickFillInIntent on the top level view of the list item instead of the list itself. Here's the code change that made it work in RemoteViewsFactory:
public RemoteViews getViewAt(int position) {
if (DEBUG) Log.d(LOGTAG, "StockRemoteViewsFactory getViewAt position="+position);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
rv.setTextViewText(R.id.text_symbol, mStockItems.get(position).getSymbol());
rv.setTextViewText(R.id.text_price, String.format("$%.2f", mStockItems.get(position).getPrice()));
Intent fillInIntent = new Intent(Intent.ACTION_MAIN);
fillInIntent.putExtra(StockAppWidgetProvider.ITEM_NUM, position);
// set on the top level view of the list item, instead of the list view itself
//rv.setOnClickFillInIntent(R.id.stocks, fillInIntent);
rv.setOnClickFillInIntent(R.id.general_layout, fillInIntent);
return rv;
}
这篇关于从AppWidget开始MainActivity不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!