0

我有一个问题,我创建了一个应该在 AlarmManager 唤醒时创建的服务。实际上它正在开始( onStartCommand被调用我看到了 toast),但在日志中同时出现:

    09-19 02:48:32.537: I/ActivityManager(1983): START {flg=0x4 cmp=com.my.app/.syncService.SyncService (has extras)} from pid -1
    09-19 02:48:39.662: W/ActivityManager(1983): Unable to start service Intent { act=com.my.app.syncService.SyncService flg=0x4 (has extras) }: not found

意图:

    Intent intent = new Intent("com.my.app.syncService.SyncService");
    PendingIntent pendingIntent = PendingIntent.getService(this, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*30, pendingIntent);

我的服务看起来像这样:

public class SyncService extends Service{

    @Override
    public void onCreate() {
        Log.d("SyncService", "onCreate");
        Log.d("SyncService", "onCreate");
        Log.d("SyncService", "onCreate");
        super.onCreate();
        Toast.makeText(this, "Debug: SyncService created", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Debug: SyncService onStartCommand", Toast.LENGTH_LONG).show();
        return super.onStartCommand(intent, flags, startId);
    }
}

安卓清单:

   <application>
       ...
       <service android:enabled="true" android:name="com.my.app.syncService.SyncService"/>       
   </application>

是不是有点奇怪?onCreate get 只调用一次,这对我来说很有意义,但其余的呢?

4

3 回答 3

0

您是否缺少 AndroidManifest.xml 中的接收器。像这样的东西:

    <receiver android:name="SyncReceiver">
        <intent-filter>
            <action android:name="com.my.app.syncService.SyncService""/>
        </intent-filter>
    </receiver>
于 2012-09-19T01:45:22.367 回答
0

我认为您可以尝试更改此行

 Intent intent = new Intent("com.my.app.syncService.SyncService");

 Intent intent = new Intent(context,SyncService.class);

在这里,这context取决于您在哪里创建AlarmManager

于 2012-09-19T02:20:42.767 回答
0
Intent intent = new Intent("com.my.app.syncService.SyncService");

AndroidManifest.xml中Intent类引用intent-filter的构造函数

于 2012-09-19T02:43:58.930 回答