gpt4 book ai didi

android - 关闭我的应用程序后未调用 BroadcastReceiver

转载 作者:行者123 更新时间:2023-11-29 19:19:31 25 4
gpt4 key购买 nike

我试图在固定时间向用户显示通知。我正在使用 BroadcastReceiver 来显示通知。问题是,BroadcastReceiver 仅在应用程序在前台或后台运行时调用。但是 BroadcastReceiver 在我关闭应用程序(意味着终止应用程序)后没有调用。我正在使用 AlarmManager 调用 BroadcastReceiver

我在 stackoverflow 上的某个地方读到,我们不提供专门用于调用 BroadcastReceiver 的服务。我们只需要在 AndroidManifest 中注册它。

我使用的是 Xiomi Redmi 3s prime 设备,android 版本是 6.0.1。

所以我缺少什么?为什么 BroadcastReceiver 没有被调用?

这是我触发 AlarmManager 的类(class)

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());

}
}

这是我的 list

<?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>

最佳答案

改用WakefulBroadcastReceiver

实现 BroadcastReceiver 的常见模式的帮助程序,它接收设备唤醒事件,然后将工作传递给 Service,同时确保设备不会停止在过渡期间回到 sleep 状态。

此类负责为您创建和管理部分唤醒锁;您必须请求 WAKE_LOCK 权限才能使用它。

参见 documentation这里

希望对您有所帮助!

关于android - 关闭我的应用程序后未调用 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com