电话重启后,警报管理器无法正常工作

时间:2017-05-26 23:56:59

标签: java android

当我设置闹钟,然后重启设备时,闹钟停止工作。我正确注册了WHERE并创建了BroadcastReciver,但在设备重启后仍然无法正常工作。

我的代码:

MainActivty首次设置闹钟:

Boot BroadCastReciver

BroadcastReciver任务:

public class MainActivity extends AppCompatActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("language","1");
        editor.commit();

        setRecurringAlarm(this);

    }

    private void setRecurringAlarm(Context context) {

        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
        updateTime.set(Calendar.HOUR_OF_DAY, 3);
        updateTime.set(Calendar.MINUTE, 00);

        Intent intent = new Intent(context, Tasks.class);
        PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarms = (AlarmManager) this.getSystemService(
                Context.ALARM_SERVICE);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                updateTime.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, recurringDownload);
    }
}

Boot BroadcastReciver:

public class Tasks extends BroadcastReceiver {

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

            Intent i = new Intent();
            i.setClassName(context, Main2Activity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
}

AndroidManifest:

public class BootBroadcastReceiver extends BroadcastReceiver {

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

        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
        updateTime.set(Calendar.HOUR_OF_DAY, 3);
        updateTime.set(Calendar.MINUTE, 00);

        Intent i = new Intent(context, Tasks.class);
        PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
                0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarms = (AlarmManager) context.getSystemService(
                Context.ALARM_SERVICE);
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                updateTime.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, recurringDownload);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要服务和广播接收器,以便在移动设备重启时唤醒您的应用。在这里,我有服务代码,可能会帮助你(它完全适用于我的应用程序)。

<强>服务

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;


public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new GPSTracker(ReunirseService.this);
        return START_STICKY;
    }
}

接收方将接收启动已完成的意图,并将启动您的服务。

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    public class BootReceiver extends BroadcastReceiver {
        RestartServiceReceiver alarm = new RestartServiceReceiver();
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
            {
             Intent i = new Intent(context, MyService .class);
             context.startService(i);
            }

        }
    }

您需要添加 RECEIVE_BOOT_COMPLETED

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

还需要添加过滤器

        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>