【问题标题】:BroadcastReceiver does not work, no notificationBroadcastReceiver 不工作,没有通知
【发布时间】:2017-09-28 20:37:30
【问题描述】:

我尝试过做一个简单的应用程序(或者,更具体地说是一个接收器):
当用户按下“相机按钮”或“经销商按钮”时 - 应用会发出通知。

没有错误,但应用程序无法运行。

我尝试过添加权限,但还是不行。

MyDownloadBroadcastReceiver.java:

package elyahsiv.raisenotificationwhendownloadfile;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
public class MyDownloadBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive (Context context, Intent intent) {
        Log.d("NOTES:", intent.getAction());
        if (intent.getAction() == "android.intent.action.PACKAGE_ADDED")
            showNotification(context);
        else if (intent.getAction() == "android.intent.action.CAMERA_BUTTON")
            showNotification(context);
    }
    private void showNotification(Context context) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);
        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                        .setSmallIcon(android.R.drawable.stat_notify_error)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="elyahsiv.raisenotificationwhendownloadfile">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyDownloadBroadcastReceiver" android:enabled="true" android:exported="false">
            <uses-permission android:name="android.permission.CAMERA" />
            <uses-feature android:name="android.hardware.telephony" android:required="false" />
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED"></action>
                <action android:name="android.intent.action.CAMERA_BUTTON"></action>
                <action android:name="android.intent.action.CALL_BUTTON"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

【问题讨论】:

  • 请发布您的错误日志。
  • 没有错误。只是,虽然我点击了按钮 -​​ 没有发出任何通知......
  • 至少正确发布接收者类
  • 对不起。不知何故,签名逃脱了我...
  • 你怎么打电话给receiver

标签: android broadcastreceiver


【解决方案1】:

必须在应用程序标记之前声明权限。 你必须拥有 android.permission.CAMERA 甚至更多。 在意图过滤器中你必须有

<data android:scheme="package" />

考虑将接收器拆分为三个不同的接收器。 还有

由于自 Android 3.1 以来广播行为发生变化,您的应用必须先启动,然后才能接收应用安装/删除意图。见歌舞伎的回答in this thread

检查此answer

尝试在创建活动或服务时注册您的接收器。 您还应该考虑以下几点:

面向 Android 8.0 或更高版本的应用无法再在其清单中为隐式广播注册广播接收器。

例如:

MyDownloadBroadcastReceiver receiver = new MyDownloadBroadcastReceiver(); 
IntentFilter receiverFilterCameraButton = new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver( receiver, receiverFilterCameraButton );

【讨论】:

  • 感谢您的详细回答。我尝试了所有方法,但由于某种原因,仍然无法正常工作..您有其他想法或方法来找出问题所在吗?
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多