Android Notification未出现在BroadcastReceiver中

时间:2017-08-30 13:03:16

标签: android notifications broadcastreceiver

我正在尝试在用户输入的秒数后创建通知。在我看来,AlarmManager和BroadcastReceiver工作正常(onReceive()中的Toast出现在正确的时间后)但我的通知不起作用(它没有出现)。

我的通知有什么问题?

这是我的代码:

MainActivity:

package com.example.user.alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

Button btn_set;
EditText et_time;

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


    btn_set = (Button) findViewById(R.id.setAlarmButton);
    et_time = (EditText) findViewById(R.id.edittext);

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

            int time = Integer.parseInt(et_time.getText().toString());

            Intent intent = new Intent(MainActivity.this, Alarm.class);
            PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time*1000, pi );

        }
    });
}
}

警报

package com.example.user.alarm;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;



public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent1) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setSmallIcon(R.drawable.ic_action_name);
    mBuilder.setContentTitle("Notification Alert, Click Me!");
    mBuilder.setContentText("Hi, This is Android Notification Detail!");

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // notificationID allows you to update the notification later on.
    mNotificationManager.notify(0, mBuilder.build());

    Toast.makeText(context, "Alarm Alarm Alarm", Toast.LENGTH_SHORT).show();
}
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.alarm">

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


    <activity
        android:name="com.example.user.alarm.MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="com.example.user.alarm.Alarm"
        />
</application>

1 个答案:

答案 0 :(得分:0)

如果您的目标API是Android O,则需要一个频道ID。