简体   繁体   English

有意图的服务无法正常工作。 需要帮助

[英]Service with intents not working. Help needed

I was having trouble getting my intents to work all the time from my main AppWidgetProvider, so I decided to move them to a service, but I cannot get it to work. 我一直无法从主AppWidgetProvider上获取自己的意图,因此我决定将其移至服务中,但无法使其正常工作。 MY intents are not firing. 我的意图没有激发。

My manifest have this to declare my service: 我的清单有此声明我的服务:

 <service android:name=".IntentService"
            android:label="IntentService">
     <intent-filter>
         <action android:name="android.tristan.widget.digiclock.action.UPDATE_2" />
     </intent-filter>
 </service>

And the code for my service is this: 我的服务代码是这样的:

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);       
         }
          }
};
};
}

I am obviously doing something wrong, I just cannot figure out what. 我显然做错了,我只是搞不清什么。

EDIT: I have this in my onUpdate() in my main class, can that be some kind if issue? 编辑:我在我的主类中的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);
}
}

Also, do I need to declare anything else in my manifest to get it working? 另外,我是否需要在清单中声明其他任何内容才能使其正常工作?

You forgot to assign your intent filter with an action 您忘记为操作分配意图过滤器

static {
    sIntentFilter = new IntentFilter();
    sIntentFilter.addAction(ACTION_UPDATE);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM