简体   繁体   English

BroadcastReceiver不起作用

[英]BroadcastReceiver does not work

I'm trying to create some simple BroadcastReceiver according to manuals but I can't to make it work. 我正在尝试根据手册创建一些简单的BroadcastReceiver ,但无法使其正常工作。 I'm calling procedure setAlarm in onCreate function in MainActivity that looks like this: 我在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);
}

It should activate AlarmReceiver that should do some action but doesn't do anything. 它应激活应该执行某些操作但不执行任何操作的AlarmReceiver It looks like this: 看起来像这样:

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

I suspect that I'm missing something in AndroidManifest but I don't know what. 我怀疑我缺少AndroidManifest中的某些内容,但我不知道该怎么办。 AndroidManifest looks like this: 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>

Ultimately appliacation should be able to send notification every day at certain time even if application isn't running at the moment. 最终,即使此时应用程序未运行,应用程序也应该能够每天在特定时间发送通知。 (I will have to then replace (我将不得不替换

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

line with setRepeating function but so far for testing purposes I need first to get it running.) But so far AlarmReceiver class doesn't do anything. 与setRepeating函数保持一致,但到目前为止,出于测试目的,我需要先使其运行。)但是到目前为止,AlarmReceiver类没有任何作用。 I'm getting some error when I try to run it (but application doesn't crash): 尝试运行它时出现一些错误(但应用程序不会崩溃):

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

I'm not sure whether the error log is related to this or something else in the project. 我不确定错误日志是否与此相关或项目中的其他内容有关。 What am I doing wrong? 我究竟做错了什么? Thanks in advance for any answer. 预先感谢您的任何答复。

Sorry I saw later your receiver in manifest, your error is that you ar getting a pending intent with getService instead of getBroadcast. 抱歉,我稍后在清单中看到了您的接收者,您的错误是您通过getService而不是getBroadcast获取待处理的意图。

Just change it to: 只需将其更改为:

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

I tried it just now :) 我刚才试过了:)

-- edited I will leave this as a reference -- -编辑,我将以此作为参考-

You have to register the receiver somehow, either by code or by xml. 您必须以某种方式通过代码或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. 警报是一个Intent广播,会发送到您在registerReceiver(BroadcastReceiver,IntentFilter)中注册的广播接收器,或者通过AndroidManifest.xml文件中的标记注册的广播接收器。

Refer to: 参考:

- Pending intent - getBroadcast - 待定意图-getBroadcast

- AlarmManager - 警报管理器

- Triggering alarm and issues - 触发警报和问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM