我正在尝试使用两个打开不同应用程序的按钮创建一个小部件。无法理解为什么按钮不起作用。有人可以帮忙吗? mainactivity java类如下所示。
我对将小部件添加到小部件感到有些困惑,但我认为我已经完成了所有操作。
package com.example.widget;
import com.example.carwidget.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MainActivity extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_activity);
Intent active = new Intent();
active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
active.addCategory(Intent.CATEGORY_LAUNCHER);
active.setAction(Intent.ACTION_MAIN);
active.setComponent(new ComponentName("com.spotify.mobile.android.ui", null));
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button1, actionPendingIntent);
active = new Intent();
active.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
active.addCategory(Intent.CATEGORY_LAUNCHER);
active.setAction(Intent.ACTION_MAIN);
active.setComponent(new ComponentName("com.google.android.apps.maps", null));
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button2, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getComponent().equals("com.spotify.mobile.android.ui")) {
context.startActivity(intent);
} else if (intent.getComponent().equals("com.google.android.apps.maps")) {
context.startActivity(intent);;
} else {
super.onReceive(context, intent);
}
}
}
答案 0 :(得分:0)
请参阅ComponentName
here的构造函数。该类的名称不能为空。
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// ......
active.setComponent(new ComponentName("com.spotify.mobile.android.ui", null));
// ......
active.setComponent(new ComponentName("com.google.android.apps.maps", null));
// ......
}