当AlarmManager触发接收器时,应用程序崩溃

时间:2016-02-17 15:03:35

标签: java android broadcastreceiver alarmmanager

我正在尝试使用AlarmManagerBroadcastReceiver每3秒运行一些代码。

这是清单:

< manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="crm.app">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

    <application ...>
        <receiver android:name=".crm.app.utilities.DeviceReceiver">
    ...
    <activity
            android:name=".activities.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />
    </application>

SettingsActivity

public void startAlarm(View view) {
    String action = btn.getText().toString();
    if(action.equals("Enabled")){
        // kill alarm task
    }else{
        // start alarm task
        Long time = 3000L;
        Intent intentAlarm = new Intent(this, DeviceReceiver.class);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Started", Toast.LENGTH_LONG).show();
    }
}

DeviceReceiver

public class DeviceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "I'm running after device booted", Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }
}

到目前为止,警报已开始,但应用程序将在大约3秒后崩溃并显示以下消息:&#34;不幸的是,应用程序已停止。&#34; 3-20秒后,我将收到此消息。

所以看起来警报正在工作但是当它试图呼叫接收器时失败。

如何让警报触发接收器并显示Toast?

编辑:这是相关的堆栈跟踪

FATAL EXCEPTION: main
Process: crm.geoalertapp, PID: 15845
java.lang.RuntimeException: Unable to start receiver crm.app.crm.app.utilities.DeviceReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
 at android.app.ActivityThread.-wrap14(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
 at crm.app.crm.app.utilities.DeviceBootReceiver.onReceive(DeviceReceiver.java:17)
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
 at android.app.ActivityThread.-wrap14(ActivityThread.java) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:148) 
 at android.app.ActivityThread.main(ActivityThread.java:5417) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

您明确地创建了意图:

        Intent intentAlarm = new Intent(this, DeviceReceiver.class);

您没有为其添加操作,因此您不应该像getAction()那样致电

intent.getAction().equals("android.intent.action.BOOT_COMPLETED")

intent.getAction()将返回null。

所以要解决您的问题,只需删除此代码:

if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        Toast.makeText(context, "I'm running after device booted", Toast.LENGTH_SHORT).show();
}