广播接收器未在三星注册
Broadcast Receiver not registering in Samsung
广播接收器
我有一个每天重复闹钟的应用程序,但不知何故在重启后它没有注册三星 Galaxy Grand Neo (Android 4.4.2) 的广播接收器,但在索尼 Xperia ZR 上工作正常 (Android5.0.2)
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sap.wish"
android:installLocation="preferExternal" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:label="@string/title_activity_fullscreen"
android:screenOrientation="portrait"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/SettingsTheme" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name=".AlarmReceiver" />
</intent-filter>
</receiver>
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".StopAlarm"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
BootUpReceiver class
package com.sap.wish;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.Calendar;
public class BootUpReceiver extends BroadcastReceiver {
AlarmManager alarmManager;
PendingIntent pendingIntent;
SQLiteDatabase db;
String tableName = "SETTINGS";
final int ALARM_ID = 1;
final String id = "ID";
final String value = "VALUE";
public BootUpReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
db = context.getApplicationContext().openOrCreateDatabase("WTDatabase", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName
+ "(" + id + " INT PRIMARY KEY NOT NULL," +
value + " TEXT NOT NULL);");
Cursor c = db.rawQuery("SELECT * FROM " + tableName, null);
if (c.moveToFirst()) {
String result = c.getString(c.getColumnIndex(value));
long savedmilli = Long.parseLong(result);
Calendar savedCalendar = Calendar.getInstance();
savedCalendar.setTimeInMillis(savedmilli);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID, intent2, 0);
if (savedmilli != 0) {
while (System.currentTimeMillis() > savedmilli)
savedmilli = savedmilli + (24 * 60 * 60 * 1000);
db.execSQL("UPDATE " + tableName + " SET " + value + "='" + String.valueOf(savedmilli) + "' WHERE " + id + "=" + 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, savedmilli, pendingIntent);
}
}
c.close();
}
}
我认为这是因为您的应用程序可能安装在 外部 SD 卡 上,如果是这样,您的 BOOT_COMPLETED
接收器将永远不会被调用。
要修复此添加:
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
你的接收者的意图过滤器是这样的:
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
希望这对您有所帮助,如果没有,可能会更改安装位置:
android:installLocation="internalOnly"
编辑
一些设备获得了快速 boot/reboot 功能并且不会触发 BOOT_COMPLETED
接收器,在这种情况下尝试使用:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
祝你好运
广播接收器
我有一个每天重复闹钟的应用程序,但不知何故在重启后它没有注册三星 Galaxy Grand Neo (Android 4.4.2) 的广播接收器,但在索尼 Xperia ZR 上工作正常 (Android5.0.2)
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sap.wish"
android:installLocation="preferExternal" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:excludeFromRecents="true"
android:label="@string/title_activity_fullscreen"
android:screenOrientation="portrait"
android:theme="@style/FullscreenTheme" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/SettingsTheme" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name=".AlarmReceiver" />
</intent-filter>
</receiver>
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".StopAlarm"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
BootUpReceiver class
package com.sap.wish;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.Calendar;
public class BootUpReceiver extends BroadcastReceiver {
AlarmManager alarmManager;
PendingIntent pendingIntent;
SQLiteDatabase db;
String tableName = "SETTINGS";
final int ALARM_ID = 1;
final String id = "ID";
final String value = "VALUE";
public BootUpReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
db = context.getApplicationContext().openOrCreateDatabase("WTDatabase", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName
+ "(" + id + " INT PRIMARY KEY NOT NULL," +
value + " TEXT NOT NULL);");
Cursor c = db.rawQuery("SELECT * FROM " + tableName, null);
if (c.moveToFirst()) {
String result = c.getString(c.getColumnIndex(value));
long savedmilli = Long.parseLong(result);
Calendar savedCalendar = Calendar.getInstance();
savedCalendar.setTimeInMillis(savedmilli);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, ALARM_ID, intent2, 0);
if (savedmilli != 0) {
while (System.currentTimeMillis() > savedmilli)
savedmilli = savedmilli + (24 * 60 * 60 * 1000);
db.execSQL("UPDATE " + tableName + " SET " + value + "='" + String.valueOf(savedmilli) + "' WHERE " + id + "=" + 1);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, savedmilli, pendingIntent);
}
}
c.close();
}
}
我认为这是因为您的应用程序可能安装在 外部 SD 卡 上,如果是这样,您的 BOOT_COMPLETED
接收器将永远不会被调用。
要修复此添加:
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
你的接收者的意图过滤器是这样的:
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
希望这对您有所帮助,如果没有,可能会更改安装位置:
android:installLocation="internalOnly"
编辑
一些设备获得了快速 boot/reboot 功能并且不会触发 BOOT_COMPLETED
接收器,在这种情况下尝试使用:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
祝你好运