1

我有一项由警报管理器启动的服务设置为重复:

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
serviceSaveSample = new Intent(RunActivity.this,SaveSampleService.class);
alarmSaveSample = PendingIntent.getService(getApplicationContext(), 9988766, serviceSaveSample, PendingIntent.FLAG_CANCEL_CURRENT);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (secondsToSample*1000) , secondsToSample*1000, alarmSaveSample);

我的 onDestory 函数:

AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if(alarmSaveSample!=null)
alarm.cancel(alarmSaveSample);
if(alarmSendSample!=null)
alarm.cancel(alarmSendSample);

// stop services
if(serviceSendSample!=null)
stopService(serviceSendSample);
if(serviceSaveSample!=null)
stopService(serviceSaveSample);

super.onDestroy();

当我退出应用程序(通过从主屏幕的内存中清除它)时,我收到错误:

 10-31 13:58:32.800: E/AndroidRuntime(24267):
 java.lang.RuntimeException: Unable to start service
 com.b2e.m2v.SaveSampleService@411b9ba8 with Intent { flg=0x4
 cmp=com.b2e.m2v/.SaveSampleService (has extras) }:
 java.lang.NullPointerException 10-31 13:58:32.800:
 E/AndroidRuntime(24267):   at
 android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2801)
 10-31 13:58:32.800: E/AndroidRuntime(24267):   at
 android.app.ActivityThread.access$1900(ActivityThread.java:153) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
 10-31 13:58:32.800: E/AndroidRuntime(24267):   at
 android.os.Handler.dispatchMessage(Handler.java:99) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 android.os.Looper.loop(Looper.java:137) 10-31 13:58:32.800:
 E/AndroidRuntime(24267):   at
 android.app.ActivityThread.main(ActivityThread.java:5227) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 java.lang.reflect.Method.invokeNative(Native Method) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 java.lang.reflect.Method.invoke(Method.java:511) 10-31 13:58:32.800:
 E/AndroidRuntime(24267):   at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
 10-31 13:58:32.800: E/AndroidRuntime(24267):   at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 dalvik.system.NativeStart.main(Native Method) 10-31 13:58:32.800:
 E/AndroidRuntime(24267): Caused by: java.lang.NullPointerException
 10-31 13:58:32.800: E/AndroidRuntime(24267):   at
 com.b2e.m2v.Controller.getLocation(Controller.java:75) 10-31
 13:58:32.800: E/AndroidRuntime(24267):     at
 com.b2e.m2v.SaveSampleService.onStartCommand(SaveSampleService.java:35)
 10-31 13:58:32.800: E/AndroidRuntime(24267):   at
 android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2784)
 10-31 13:58:32.800: E/AndroidRuntime(24267):   ... 10 more

知道为什么吗?

也许重要的是要说这段代码在 FragmentActivity 中运行

4

3 回答 3

3
 public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
     }     




 public void CancelAlarm(Context context)
         {
             Intent intent = new Intent(context, Alarm.class);
             PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
             alarmManager.cancel(sender);
         }
于 2013-10-31T12:22:05.043 回答
1

正如您从堆栈跟踪中看到的那样,您NullPointerExceptionController课堂上遇到了 a 。

 E/AndroidRuntime(24267): Caused by: java.lang.NullPointerException
10-31 13:58:32.800: E/AndroidRuntime(24267):   at com.b2e.m2v.Controller.getLocation(Controller.java:75) 

这意味着那里出了问题。从您的评论中,我了解到该代码看起来有点像这样:

public Location getLocation(){ 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    return location;
}

这意味着您的变量locationManagernull您尝试获取它的时候。

于 2013-10-31T12:23:38.093 回答
0

换行

alarmSaveSample = PendingIntent.getService(getApplicationContext(), 9988766, serviceSaveSample, PendingIntent.FLAG_CANCEL_CURRENT);

alarmSaveSample = PendingIntent.getBroadcast(getApplicationContext(), 9988766, serviceSaveSample, PendingIntent.FLAG_CANCEL_CURRENT);

广播服务_

出于某种原因,尽管在大约无数不同的位置取消了前者,但它会不断地无限期地重新唤醒服务。YMMV

于 2015-02-26T17:13:10.600 回答