Android Notification不会触发BroadcastReceiver来启动后台操作

时间:2017-03-11 22:19:54

标签: android notifications

我无法在点击Android通知中的按钮时执行操作。我需要此操作在后台,不应该打开活动。我最终试图记录数据使用情况,但现在只需要让BroadcastReceiver工作。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent resultIntent = new Intent(this, BlankActivity.class);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                this,
                (int) System.currentTimeMillis(),
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

        Intent mIntent = new Intent(this, StartRecording.class);
        PendingIntent mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, mIntent , PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setContentTitle("Monitor Resources")
                        .setContentText("Choose your action")
                        .setSmallIcon(R.drawable.opened_email_envelope)
                        .addAction(R.drawable.opened_email_envelope, "Start", mPendingIntent)
                        .setContentIntent(mPendingIntent)
                        .addAction(R.drawable.opened_email_envelope, "Stop", resultPendingIntent);

        int mId = 001;
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(mId, mBuilder.build());

}

class StartRecording extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("MonitorSpeed", "BroadcastReceiver Started");
        Toast.makeText(context, "Started!", Toast.LENGTH_SHORT).show();
    }
}

我也以这种方式在清单文件中注册了服务:

<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=".BlankActivity"
        android:label="@string/title_activity_blank"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
<receiver android:name=".helper.NotificationReceiver" />
<receiver
    android:name=".StartRecording">
    <intent-filter>
        <action android:name="com.example.sd.monitorspeed.BROADCAST" />
    </intent-filter>
</receiver>

我做错了什么?任何人都可以帮我指出错误吗?

0 个答案:

没有答案