gpt4 book ai didi

java - 广播接收器不起作用

转载 作者:行者123 更新时间:2023-12-01 10:03:36 26 4
gpt4 key购买 nike

我正在尝试根据手册创建一些简单的BroadcastReceiver,但我无法使其工作。我在 MainActivity 的 onCreate 函数中调用过程 setAlarm,如下所示:

private void setAlarm() {
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);
}

它应该激活 AlarmReceiver,它应该执行一些操作,但不执行任何操作。它看起来像这样:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_LONG).show();
notifikace(context);
}

public void notifikace(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.notifikace);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("aaa");
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, mBuilder.build());
}
}

我怀疑我在 AndroidManifest 中遗漏了一些东西,但我不知道是什么。 AndroidManifest 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Nastaveni"
android:label="@string/title_activity_nastaveni"
android:theme="@style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name=".Tyden"
android:label="@string/title_activity_tyden"
android:theme="@style/AppTheme.NoActionBar"></activity>
<receiver android:name=".AlarmReceiver" >
</receiver>
</application>

最终,即使应用程序当前未运行,应用程序也应该能够每天在特定时间发送通知。 (我将不得不更换

manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);

与 setRepeating 函数一致,但到目前为止,出于测试目的,我需要首先让它运行。)但到目前为止 AlarmReceiver 类没有做任何事情。当我尝试运行它时遇到一些错误(但应用程序不会崩溃):

04-14 11:52:54.592 1300-1354/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.xxx.myapplication3/com.example.xxx.myapplication.MainActivity (has extras)} from uid 10014 on display 0
04-14 11:52:54.673 1300-1635/? I/ActivityManager: START u0 {act=android.content.pm.action.REQUEST_PERMISSIONS pkg=com.android.packageinstaller cmp=com.android.packageinstaller/.permission.ui.GrantPermissionsActivity (has extras)} from uid 10057 on display 0

我不确定错误日志是否与此相关或项目中的其他内容相关。我究竟做错了什么?预先感谢您的回答。

最佳答案

抱歉,我后来在 list 中看到了您的接收器,您的错误是您使用 getService 而不是 getBroadcast 获取待处理的 Intent 。

只需将其更改为:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

我刚刚尝试过:)

--已编辑,我将将此作为引用--

您必须以某种方式注册接收器,无论是通过代码还是通过 xml。

The alarm is an Intent broadcast that goes to a broadcast receiver that you registered with registerReceiver(BroadcastReceiver, IntentFilter) or through the tag in an AndroidManifest.xml file.

引用:

-Pending intent - getBroadcast

-AlarmManager

-Triggering alarm and issues

关于java - 广播接收器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36626458/

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