我正在尝试实现一个集合小部件,当我点击其中一个项目时,它会启动我的应用程序的主要活动。出于某种原因,列表上的视图是可点击的(我在看到时会看到涟漪效应),但没有启动任何意图。
你能帮我找到我错的地方吗?
这是小部件的实现
public class StockWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = initViews(context, appWidgetManager, appWidgetId);
Intent intent = new Intent(context, MyStocksActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final PendingIntent pendingIntent = PendingIntent
.getActivity(context,0,intent,0);
views.setPendingIntentTemplate(R.id.widgetCollectionList,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context,appWidgetManager,appWidgetIds);
}
private RemoteViews initViews(Context context, AppWidgetManager widgetManager, int widgetId) {
RemoteViews mView = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Intent intent = new Intent(context, StockWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
mView.setRemoteAdapter(R.id.widgetCollectionList, intent);
return mView;
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
这是创建和传递远程视图的类:
public class WidgetDataProvider implements RemoteViewsService.RemoteViewsFactory {
List<String> mCollections = new ArrayList<>();
Context mContext = null;
public WidgetDataProvider(Context mContext, Intent intent) {
this.mContext = mContext;
}
@Override
public int getCount() {
return mCollections.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews mView = new RemoteViews(
mContext.getPackageName(),
android.R.layout.simple_list_item_1);
mView.setTextViewText(android.R.id.text1, mCollections.get(position));
mView.setTextColor(android.R.id.text1, Color.BLACK);
final Intent fillInIntent = new Intent(mContext, MyStocksActivity.class);
fillInIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mView.setOnClickFillInIntent(android.R.id.text1,fillInIntent);
return mView;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onCreate() {
initData();
}
@Override
public void onDataSetChanged() {
initData();
}
@Override
public void onDestroy() {
}
private void initData() {
mCollections.clear();
for (int i=0; i<10;i++) {
mCollections.add("Listview item" + i);
}
}
}
这是请求窗口小部件的远程视图的服务:
public class StockWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new WidgetDataProvider(getApplicationContext(),intent);
}
}
P.S。:我宣布了清单中的所有组件。
答案 0 :(得分:-1)
你创造了你的意图,但你永远不会发动它。
final Intent fillInIntent = new Intent(mContext, MyStocksActivity.class);
fillInIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(fillInIntent);
必须调用startActivity()
我也不熟悉这一行
mView.setOnClickFillInIntent(android.R.id.text1,fillInIntent);
但如果您尝试设置点击监听器,则需要调用startActivity(fillInIntent);从那个听众内部