2017-06-28 92 views
2

我在服務內部有廣播接收器。該服務應啓動報警管理器,廣播接收器應接收報警。設備重新啓動後,該服務由另一個廣播接收器啓動。但我的代碼給出了以下異常和應用程序崩潰。我怎樣才能解決這個問題?服務內部的廣播接收器不起作用

06-28 17:26:20.983 6924-6924/com.example.krishan.timertest E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.krishan.timertest, PID: 6924 
    java.lang.RuntimeException: Unable to instantiate receiver com.example.krishan.timertest.MyService$AlarmReceiver: java.lang.InstantiationException: class com.example.krishan.timertest.MyService$AlarmReceiver has no zero argument constructor 
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2992) 
     at android.app.ActivityThread.access$1800(ActivityThread.java:177) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5910) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
    Caused by: java.lang.InstantiationException: class com.example.krishan.timertest.MyService$AlarmReceiver has no zero argument constructor 
     at java.lang.Class.newInstance(Class.java:1681) 
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2987) 
     at android.app.ActivityThread.access$1800(ActivityThread.java:177)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:135)  
     at android.app.ActivityThread.main(ActivityThread.java:5910)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at java.lang.reflect.Method.invoke(Method.java:372)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)  
    Caused by: java.lang.NoSuchMethodException: <init> [] 
     at java.lang.Class.getConstructor(Class.java:531) 
     at java.lang.Class.getDeclaredConstructor(Class.java:510) 
     at java.lang.Class.newInstance(Class.java:1679) 
     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2987)  
     at android.app.ActivityThread.access$1800(ActivityThread.java:177)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:135)  
     at android.app.ActivityThread.main(ActivityThread.java:5910)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at java.lang.reflect.Method.invoke(Method.java:372)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

我的服務

public class MyService extends Service { 

    public MyService() { 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     Log.d("timer test", "*********************" + " Service Started " + "*********************"); 
     startAlert(); 

     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    public void startAlert() { 
     Intent intent = new Intent(this, AlarmReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (60 * 1000), pendingIntent); 
     Toast.makeText(this, "Alarm set in 60 seconds", Toast.LENGTH_LONG).show(); 
    } 

    public class AlarmReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("Timer Test", "*********************" + " Alarm" + "*********************"); 
     } 
    } 
} 

我的廣播接收器觸發時啓動完成

public class BootReceiver extends BroadcastReceiver { 

    private Intent ServiceIntent; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     ServiceIntent = new Intent(context, MyService.class); 
     context.startService(ServiceIntent); 


     Toast.makeText(context, "Boot Receiver", Toast.LENGTH_LONG).show(); 
     Intent intentx = new Intent(context, MainActivity.class); 
     intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(intentx); 

    } 

} 

我的清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.krishan.timertest"> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service 
      android:name=".MyService" 
      android:enabled="true" 
      android:exported="true"></service> 
     <receiver android:name=".BootReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" > 
       </action> 
      </intent-filter> 
     </receiver> 
     <receiver android:name=".MyService$AlarmReceiver"></receiver> 
    </application> 

</manifest> 

如果我將廣播接收器聲明爲單獨的類,這可以正常工作。但我想在服務中聲明它。

+0

請檢查天氣您的服務是否正在開始或未開始。 –

回答

1

AlarmReceiver類應該是靜態的, 希望它幫助。

+0

是的!有效。非常感謝你。 –

0

我沒有類似的東西,登記batteryBroadcast和ScreenOn/ScreenOf廣播過,檢查代碼:

public class turnOnScreenService extends Service { 



    private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String strAction = intent.getAction(); 

      if (strAction.equals(Intent.ACTION_SCREEN_OFF)) { 
       //do stuff 
      } else if (strAction.equals(Intent.ACTION_SCREEN_ON)) { 
       //do more stuff 
      } 
     } 
    }; 

    private BroadcastReceiver mBatteryStateReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

      if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { 
       // more stuff 

      } else { 
       // more stuff 
      } 
     } 
    }; 


    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     IntentFilter screenStateFilter = new IntentFilter(); 
     screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); 
     screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); 
     registerReceiver(mScreenStateReceiver, screenStateFilter); 


     IntentFilter ifilter = new IntentFilter(); 
     ifilter.addAction(Intent.ACTION_POWER_CONNECTED); 
     ifilter.addAction(Intent.ACTION_POWER_DISCONNECTED); 
     registerReceiver(mBatteryStateReceiver, ifilter); 
    } 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.i("LocalService", "Received start id " + startId + ": " + intent); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     unregisterReceiver(mScreenStateReceiver); 
     unregisterReceiver(mBatteryStateReceiver); 
    } 


} 
+0

我應該在這裏用intentFilter.addAction()來操作什麼? –

+0

Intent.ACTION_BOOT_COMPLETED 請提出答案 –

+0

Boot接收廣播接收器沒有問題。讓服務內部的內部類使我的問題得到解決。 –