问题
I am currently working on a application which includes Boot_Completed Broadcast receiver concept. I have tested this app in my Motorola Moto G Phone. The app runs fine and shows the Toast message. But when I test this app in XIAOMI Redmi 1S phone it doesn't show the Toast message.
I have already seen many questions similar to my issue (like these - Question 1, Question 2, etc.) ... But I haven't got any solution to this problem.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demoapp"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demoapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.demoapp.MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equalsIgnoreCase(intent.getAction())) {
Toast.makeText(context, "Boot Complete.", Toast.LENGTH_LONG).show();
}
}
}
How can I solve this?
回答1:
This is solution for your bug.
What you need to do is that , you need to mention this permission also in your Intent-Filter.
<receiver android:name="com.example.demoapp.MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
Here you need to do one thing also.
Go to "Settings-Apps_open your app info-Manage permisson" in yourdevice
Here check all things here.
Because for some devices it will not work. Ex.Red mi
Try this !
回答2:
in case of XIOMI devices you have to manually set permission, i think you can solve this by adding your app in "auto-start management" list, which is available as default security app.
回答3:
Go to manage app permission
Select your app from apps
toggle "Auto-Start"
done
回答4:
try removing if condition from broadcast reciever
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Boot Complete.", Toast.LENGTH_LONG).show();
}
}
来源:https://stackoverflow.com/questions/27792615/boot-complete-is-not-working-in-android-redmi