此程序检测用户是否更改了root访问状态。用户定义任何时间段。在此时间段内,它控制rootaccess状态。当我启动服务时,它会出错。实际上它以前工作但现在不是。我找不到任何null变量。 我在设备上打开Application Manager并查看正在运行的服务。它说“再次开始”这项服务。
RootAccess服务;
public class RootAccessService extends Service{
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent,int startId) {
Bundle b= intent.getExtras();
if(b != null){
context = getApplicationContext();
helper = new SQLiteHelper(context);
timer = new Timer();
handler = new Handler();
inventoryId = 1;
periodSelection = b.getString("period");
parts = periodSelection.split(" ");
min = Long.parseLong(parts[0]);
time = min * 60 * 1000;
killMe = false;
handler.postDelayed(check, time);
}
}
private final Runnable check = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(killMe)
return;
Start();
handler.postDelayed(check, time);
}
};
@Override
public void onDestroy(){
Stop();
super.onDestroy();
}
public void Start(){
//do smtg
}
public void Stop(){
handler.removeCallbacksAndMessages(check);
killMe = true;
helper.close();
}
}
12-18 14:02:24.783: E/AndroidRuntime(18004): FATAL EXCEPTION: main
12-18 14:02:24.783: E/AndroidRuntime(18004): java.lang.RuntimeException: Unable to start service com.example.cryptodlp.RootAccessService@41de6fd0 with null: java.lang.NullPointerException
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2553)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.ActivityThread.access$2000(ActivityThread.java:134)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.os.Looper.loop(Looper.java:137)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.ActivityThread.main(ActivityThread.java:4867)
12-18 14:02:24.783: E/AndroidRuntime(18004): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 14:02:24.783: E/AndroidRuntime(18004): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 14:02:24.783: E/AndroidRuntime(18004): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-18 14:02:24.783: E/AndroidRuntime(18004): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-18 14:02:24.783: E/AndroidRuntime(18004): at dalvik.system.NativeStart.main(Native Method)
12-18 14:02:24.783: E/AndroidRuntime(18004): Caused by: java.lang.NullPointerException
12-18 14:02:24.783: E/AndroidRuntime(18004): at com.example.cryptodlp.RootAccessService.onStart(RootAccessService.java:49)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.Service.onStartCommand(Service.java:450)
12-18 14:02:24.783: E/AndroidRuntime(18004): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2536)
12-18 14:02:24.783: E/AndroidRuntime(18004): ... 10 more
答案 0 :(得分:0)
我搜索了这个问题并使用了AlarmManager并使用AlarmManager启动了服务。
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(mContext, RootAccessService.class);
PendingIntent pintent = PendingIntent.getService(mContext, 0, intent, 0);
AlarmManager alarm = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), Integer.parseInt(parts[0])*60*1000, pintent);