[英]How to start BroadcastReceiver in boot?
I have an application that start every day at 10 o'clock, and everything Work fine. 我有一个每天10点开始运行的应用程序,一切正常。 But when device reboot BroadcastReceiver doesn't work because app terminated then I start app in boot.
但是当设备重启时,因为应用终止,BroadcastReceiver无法正常工作,然后我在引导中启动了应用。 When I set it to start in Boot (I mean start BroadcastReceiver) then BroadcastReceiver don't work correctly.
当我将其设置为在Boot中启动时(我的意思是启动BroadcastReceiver),那么BroadcastReceiver将无法正常工作。
I mean my app start automatically in boot, but the time of app to start is 10 o'clock. 我的意思是我的应用程序会在启动时自动启动,但是应用程序的启动时间是10点。
This my code 这是我的代码
AndroidMaifest AndroidMaifest
<application
android:allowBackup="true"
android:icon="@drawable/aps"
android:label="@string/app_name"
android:roundIcon="@drawable/aps"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:theme="@style/AppTheme.NoActionBar"></activity>
<receiver android:name=".Tasks">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
Mainactivity 主要活动
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}
BroadCastReciver BroadCastReciver
public class Tasks extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
Intent i = new Intent();
i.setClassName(context, Main2Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
You defined the same Receiver
both for the Alarm
in your activity and for BOOT_COMPLETED
event; 您为活动中的
Alarm
和BOOT_COMPLETED
事件定义了相同的Receiver
; So when the alarm trigger or system boot the same class is called (Which Start the Main Activity). 因此,当警报触发器或系统启动时,将调用同一类(启动主活动)。
You need to define two Receiver
. 您需要定义两个
Receiver
。 One to Receive Alarm and One to Set Alarm on Boot Complete , for Example: 一个在启动完成时 接收警报 ,另一个在设置警报时设置警报 ,例如:
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 intent = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}
, and for your Alarm
Receiver you can have your old Receiver: ,对于您的
Alarm
接收器,您可以使用旧的接收器:
public class AlarmReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
Intent i = new Intent();
i.setClassName(context, Main2Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Use two different BroadcastReceiver
. 使用两个不同的
BroadcastReceiver
。 One for alarm
receiver and another for boot
complete. 一个用于
alarm
接收器,另一个用于boot
完成。
Here is your BootCompletedReceiver
: 这是您的
BootCompletedReceiver
:
public class BootCompletedReceiver 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 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);
}
}
Update your AndroidManifest.xml
as below: 如下更新您的
AndroidManifest.xml
:
<application
android:allowBackup="true"
android:icon="@drawable/aps"
android:label="@string/app_name"
android:roundIcon="@drawable/aps"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".Tasks">
</receiver>
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.