我正在尝试在固定时间向用户显示通知。我正在使用BroadcastReceiver
来显示通知。问题是,BroadcastReceiver
仅在应用程序在前台或后台运行时调用。但是当我关闭应用程序时,BroadcastReceiver
没有被调用(意味着杀死应用程序)。我使用AlarmManager
来致电BroadcastReceiver
。
我在stackoverflow上的某处读到,我们不打算专门为调用BroadcastReceiver
提供服务。我们只需要在AndroidManifest
注册它。
我正在使用Xiomi Redmi 3s prime设备,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>
答案 0 :(得分:2)
使用WakefulBroadcastReceiver
代替
帮助实现接收设备唤醒事件的BroadcastReceiver
的通用模式,然后将工作传递给Service
,同时确保设备在转换期间不会重新进入休眠状态
该课程负责为您创建和管理部分唤醒锁;您必须请求WAKE_LOCK
权限才能使用它。
请参阅此处的documentation
希望它有所帮助!!
答案 1 :(得分:0)
在活动中: -
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this,MyFirstService.class));
}
在服务中: -
公共类MyFirstService扩展了Service {
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
Notification note = new Notification(0,null,System.currentTimeMillis());
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground(42, note);
stopService(new Intent(this, MyService.class));
stopForeground(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
}
}
在广播中: -
广播在创建serivce之后编写代码。