我試圖在固定時間向用戶顯示通知。我正在使用BroadcastReceiver
來顯示通知。問題在於,BroadcastReceiver
僅在應用程序在前臺或後臺運行時調用。但是當我關閉應用程序(意味着關閉應用程序)後不會調用BroadcastReceiver
。我正在使用AlarmManager
致電BroadcastReceiver
。關閉我的應用程序後未調用BroadcastReceiver
我在某個地方讀到了stackoverflow,我們沒有專門打電話給BroadcastReceiver
。我們只需要在AndroidManifest
中註冊它。
我使用的是秀美紅米3s主裝置和android版本是6.0.1。
那麼我錯過了什麼?爲什麼BroadcastReceiver
不叫?
這是我在哪裏觸發AlarmManager
public class Settings extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.settings,container,false);
Intent intent = new Intent(getActivity(), BroadcastReciever.class);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Service.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 27);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
return view;
}
}
這是我的課我BroadcastReceiver
public class BroadcastReciever extends BroadcastReceiver {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onReceive(Context context, Intent intent) {
Intent nofificationIntent = new Intent(context, Home.class);
nofificationIntent.putExtra("notify_key","notify_value");
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,1,
nofificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.donate)
.setSound(alarmSound)
.setContentIntent(resultPendingIntent)
.setContentTitle("Whats up")
.setContentText("Hello world");
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}
這是我的清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.nf">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Home"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="portrait"/>
<activity android:name=".AndroidDatabaseManager"
android:theme="@style/Theme.AppCompat.Light"/>
<receiver android:name="broadcast.BroadcastReciever"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NOTIFY" />
</intent-filter>
</receiver>
</application>
</manifest>
我在其他手機上測試了我的應用程序,並在其他手機上正常工作。 mi設備中可能存在一些內部定製miui os,它正在停止廣播以進行呼叫。 –