BroadcastReceiver没有在通知操作点击时触发 [英] BroadcastReceiver not firing on notification action click

本文介绍了BroadcastReceiver没有在通知操作点击时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的通知,并为其定义一个按钮(动作).我设法正确显示它并为我的操作创建一个PendingIntent.我还创建了一个BroadcastReceiver,应该在单击我的动作时调用它.但是它的onReceive()方法没有被调用.我不知道为什么.我还在AndroidManifest.xml中注册了BroadcastReceiver

I am trying to create a simple notification with a button (action) defined to it. I have managed to display it properly and create a PendingIntent for my action. I have also created a BroadcastReceiver which is supposed to be called when my action is clicked. But it's onReceive() method does not get called. I have no idea why. I also registered BroadcastReceiver in AndroidManifest.xml

MainActivity.java

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button notify = (Button) findViewById(R.id.notify);

    notify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent dismissIntent = new Intent("action1");
            dismissIntent.setAction("action1");
            PendingIntent action1intent = PendingIntent.getBroadcast(MainActivity.this, (int) System.currentTimeMillis(), dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_menu)
                            .setContentTitle("My notification")
                            .setContentText("Hello World!")
                            .addAction(R.drawable.ic_menu, "action 1", action1intent);

            Intent resultIntent = new Intent(MainActivity.this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(0, mBuilder.build());
        }
    });

}

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("log", "Receiving somthn");

        String whichAction = intent.getAction();

        if (whichAction.equals("action1")) {
            MainActivity.makeToast("This is action 1", context);
        }
    }
}


public static void makeToast(String string, Context context) {
    Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
}
}

AndroidManifest.xml

AndroidManifest.xml

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

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

    <receiver android:name=".MainActivity$Receiver">
        <intent-filter>
            <action android:name="com.example.orglce.notification.BROADCAST" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

推荐答案

首先,如果要使用.MainActivity$Receiver,则Receiver必须是static类.

First, if you are going to use .MainActivity$Receiver, then Receiver needs to be a static class.

第二,dismissIntent(冗余地)使用action1作为操作字符串,但是<intent-filter>使用com.example.orglce.notification.BROADCAST.这些不匹配.我建议您摆脱<intent-filter>,摆脱action1,并使用new Intent(this, Receiver.class)创建一个明确的Intent来标识您的BroadcastReceiver.

Second, dismissIntent is (redundantly) using action1 as the action string, but the <intent-filter> uses com.example.orglce.notification.BROADCAST. These do not match. I recommend getting rid of the <intent-filter>, getting rid of the action1, and using new Intent(this, Receiver.class) to create an explicit Intent to identify your BroadcastReceiver.

这篇关于BroadcastReceiver没有在通知操作点击时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆