调度alarmmanager没有工作重启我的手机

时间:2018-04-12 09:24:00

标签: java android alarmmanager alarm android-broadcastreceiver

在我没有重新启动手机之前,它一直在烘烤,但在重新启动后,broadcastreceiver2没有收到,也没有任何反应。

我跟着http://stacktips.com/tutorials/android/how-to-start-an-application-at-device-bootup-in-android和其他许多人,但没有任何事情发生。

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.k2.alarmmanagerdemo">
    <uses-permission android:name="android.permission.VIBRATE" />
    <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>
        <receiver android:name="com.k2.alarmmanagerdemo.MyBroadcastReceiver"
            android:enabled="true">
        </receiver>
        <receiver android:name="com.k2.alarmmanagerdemo.BroadCastRecevier2"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

MainActivity.java

public class MainActivity extends Activity {
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.button1);

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startAlert();
            }
        });
    }


    public void startAlert() {
        EditText text = (EditText) findViewById(R.id.time);
        int i = Integer.parseInt(text.getText().toString());
        Intent intent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),
                1000 * 5,pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
    }
}

MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

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

        Toast.makeText(context, "Alarm...." + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
        Log.i("Alarm.", "alarm called" + System.currentTimeMillis());    

    }
}

BroadCastRecevier2.java

public class BroadCastRecevier2 extends BroadcastReceiver {

    MainActivity activity = new MainActivity();
    @Override
    public void onReceive(Context context, Intent intent) {


        activity.startAlert();
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent i = new Intent();
            i.setClassName("com.k2.alarmmanagerdemo",
                    "com.k2.alarmmanagerdemo.MainActivity");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            Toast.makeText(context, "BOOT", Toast.LENGTH_SHORT).show();
            Log.i("myboot","boot compleated inside");

        }
    }
}

1 个答案:

答案 0 :(得分:1)

<receiver>的{​​{1}}声明中删除此行:

BroadcastReceiver2

android:permission="android.permission.RECEIVE_BOOT_COMPLETED" 标记中的该行告诉系统,只有拥有此权限的软件包才能向您的类发送<receiver>。它可能会使系统混乱并阻止Intent被发送到您的接收器。

在了解警报和启动完成接收器时,您可能会发现this article很有帮助。