我无法让我的意图始终从我的主AppWidgetProvider工作,所以我决定将它们转移到服务中,但我无法让它工作。我的意图不是解雇。
我的清单有这个声明我的服务:
<service android:name=".IntentService"
android:label="IntentService">
<intent-filter>
<action android:name="android.tristan.widget.digiclock.action.UPDATE_2" />
</intent-filter>
</service>
我的服务代码是:
public class IntentService extends Service {
static final String ACTION_UPDATE = "android.tristan.widget.digiclock.action.UPDATE_2";
public final static IntentFilter sIntentFilter;
public int layoutID = R.layout.clock;
private static final String LOGTAG = "tristan's DigiClock";
int appWidgetIds = 0;
static {
sIntentFilter = new IntentFilter();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onCreate() {
super.onCreate();
registerReceiver(onClickTop, sIntentFilter);
registerReceiver(onClickBottom, sIntentFilter);
Log.d(LOGTAG, "IntentService Started.");
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(onClickTop);
unregisterReceiver(onClickBottom);
Log.d(LOGTAG, "IntentService Stopped.");
}
public final BroadcastReceiver onClickTop = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK"))
{
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (NameNotFoundException e) {
Log.d("Error, ", vendor + " does not exist");
}
}
if (foundClockImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
views.setOnClickPendingIntent(R.id.TopRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmClockIntent);
}
}
}
};
public final BroadcastReceiver onClickBottom = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK_2"))
{
PackageManager calendarManager = context.getPackageManager();
Intent calendarIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String calendarImpls[][] = {
{"HTC Calendar", "com.htc.calendar", "com.htc.calendar.LaunchActivity" },
{"Standard Calendar", "com.android.calendar", "com.android.calendar.LaunchActivity"},
{"Moto Blur Calendar", "com.motorola.blur.calendar", "com.motorola.blur.calendar.LaunchActivity"}
};
boolean foundCalendarImpl = false;
for(int i=0; i<calendarImpls.length; i++) {
String vendor = calendarImpls[i][0];
String packageName = calendarImpls[i][1];
String className = calendarImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = calendarManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
calendarIntent.setComponent(cn);
foundCalendarImpl = true;
} catch (NameNotFoundException e) {
Log.d("Error, ", vendor + " does not exist");
}
}
if (foundCalendarImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views2 = new RemoteViews(context.getPackageName(), layoutID);
views2.setOnClickPendingIntent(R.id.BottomRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views2);
calendarIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calendarIntent);
}
}
};
};
}
我显然做错了什么,我只是弄不清楚是什么。
编辑:我在我的主课程中的onUpdate()中有这个,如果有问题可以这样吗?@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(UpdateService.ACTION_UPDATE));
context.startService(new Intent(context, ScreenUpdateService.class));
context.startService(new Intent(IntentService.ACTION_UPDATE));
final int Top = appWidgetIds.length;
final int Bottom = appWidgetIds.length;
for (int i=0; i<Top; i++)
{
int appWidgetId = appWidgetIds[i];
final RemoteViews top=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
top.setOnClickPendingIntent(R.id.TopRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, top);
}
for (int j=0; j<Bottom; j++)
{
int appWidgetId = appWidgetIds[j];
RemoteViews bottom=new RemoteViews(context.getPackageName(), layoutID);
Intent clickintent=new Intent("android.tristan.widget.digiclock.CLICK_2");
PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, 0);
bottom.setOnClickPendingIntent(R.id.BottomRow, pendingIntentClick);
appWidgetManager.updateAppWidget(appWidgetId, bottom);
}
}
另外,我是否需要在清单中声明其他任何内容才能使其正常工作?
答案 0 :(得分:0)
您忘记为您的意图过滤器分配一个动作
static {
sIntentFilter = new IntentFilter();
sIntentFilter.addAction(ACTION_UPDATE);
}