我有一个跟随BroadcastReceiver,应该在启动完成后运行.我已经在我的小米设备(Redmi 1s)上进行了测试,它没有运行,而在像三星这样的其他设备上它按预期运行.
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "I am Running", Toast.LENGTH_SHORT).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已在Manifest中设置了权限.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Run Code Online (Sandbox Code Playgroud)
以下是我的广播接收器:
<receiver android:name=".receiver.DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud) android broadcastreceiver android-permissions android-reboot android-broadcastreceiver
我之前已经多次使用过adb reboot,绝对没有问题.但是出于某种原因,adb reboot只是冻结了我的Gennymotion模拟器实例(API 18 - Android 4.3),我不知道如何解决这个问题.我尝试在进行adb重启时查看logcat,但这并没有指出任何有趣的事情,除非我错过了一些东西.有人可以帮忙吗?
另一个指针,如果它有帮助,我的模拟器设备会一直提示Google Play服务已停止工作.在我之前使用Gennymotion和adb进行的尝试中,我曾多次遇到过这个问题,并且只是更新所有Google应用程序过去都像是一个魅力.但是,这一次,即使这似乎也不起作用.
请原谅我这样一个基本问题.我知道当我的应用程序进程启动时,Application类会被实例化,而且我知道手机完成后会启动我的启动接收器.我假设,因为手机通过清单知道我的应用程序持有BOOT_COMPLETED意图过滤器,重启过程是.电话重新启动,电话用BOOT_COMPLETED启动所有进程,手机触发BOOT_COMPLETED广播.我担心的是,如果接收器在我的Application类实例化之前被调用,我是否在引导接收器中引用Application类实例变量.
如果这显然已经死了,请原谅我.我从未完全理解重启机制.
我在按钮点击时创建了警报管理器。但是手机重启后就不行了。我的 AlarmbroadcastReceiver 在手机重启时没有调用。它在电话锁定时工作,应用程序被杀死但在电话重启后不起作用我创建了一个进度条,它在按钮点击时启动并在警报广播触发后停止,但在手机重启时它不会停止。我已经添加了我的按钮点击事件和广播接收器类
按钮点击事件
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pb1.setVisibility(View.VISIBLE);
progress_edit.putBoolean("progress_one", true);
progress_edit.apply();
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class);
intnt.setAction("com.ex.Alarm");
PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
Log.d("Broadcast ","Fired");
}
});
Run Code Online (Sandbox Code Playgroud)
广播接收器类
@Override
public void onReceive(Context context, Intent intent) {
Log.d("inside","broadcast receive");
if(intent.getAction().equalsIgnoreCase("com.ex.Alarm"))
{
enterSys_progress_edit.putBoolean("progress_one", false);
enterSys_progress_edit.apply();
Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.krutarth.alarm">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action …Run Code Online (Sandbox Code Playgroud) 我想在设备启动时启动警报,为此我做了以下事情
1)用户权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)
2)在清单文件中添加带有意图操作的接收者
<receiver
android:name=".sms.BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
3)来源
public class BootReceiver extends BroadcastReceiver {
private AlarmManager dayAlarmMgr;
private PendingIntent dayAlarmIntent;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
private Context context;
public static final int NOTIFICATION_ID = 2;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "FIRED BOOT COMPLETE" , Toast.LENGTH_LONG).show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以在 genymotion 中运行,但不能在真实设备上运行