我正在尝试使用为其定义的按钮(操作)创建一个简单的通知。我已设法正确显示它并为我的操作创建一个PendingIntent。我还创建了一个BroadcastReceiver,它应该在单击我的动作时被调用。但它的onReceive()方法不会被调用。我不知道为什么。我还在AndroidManifest.xml中注册了BroadcastReceiver
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
<?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>
答案 0 :(得分:2)
首先,如果您要使用.MainActivity$Receiver
,则Receiver
必须是static
类。
其次,dismissIntent
(冗余地)使用action1
作为操作字符串,但<intent-filter>
使用com.example.orglce.notification.BROADCAST
。这些不匹配。我建议删除<intent-filter>
,删除action1
,然后使用new Intent(this, Receiver.class)
创建一个明确的Intent
来标识您的BroadcastReceiver
。