1
我試圖創建一個使用AlarmManager和廣播接收器的通知的通知。 (API 19,KitkKat)創建具有廣播接收器和AlarmManager
我下面的步驟教程https://www.youtube.com/watch?v=gm5n_hRIR-c (開始於10:51)
但我不能讓應用程序正常運行。沒有投擲通知。 :/ 幫助,這裏是我的代碼。
我的MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnN = (Button) findViewById(R.id.btnalarm);
btnN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Long alertOfTime = new GregorianCalendar().getTimeInMillis() + 5 * 1000;
Intent alertIntent = new Intent(this, Alert.class);
AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
aManager.set(AlarmManager.RTC_WAKEUP,alertOfTime,
PendingIntent.getBroadcast(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));
}
});
}
我班警報
public class Alert extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification (context,"NATURE TIP","When","Alert");
}
public void createNotification (Context context ,String msgg ,String msgAlert,String msgText){
PendingIntent pendIntentTwo = PendingIntent.getActivity(context,0,
new Intent(context,MainActivity.class),0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.tree_24_24)
.setContentTitle(msgg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(pendIntentTwo);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager nNotifManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);//
nNotifManager.notify(1,mBuilder.build());
}
}
彌MainLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@drawable/backk">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm Notif"
android:id="@+id/btnalarm"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
和我Manifiest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="com.android.alarm.permission.SET_ALARMY"/>
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="App Nature"
android:theme="@style/Theme.AppCompat.Light"
>
<activity
android:name=".MainActivity"
android:label="App Nature" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你的警報類不在清單,你需要添加'<接收 機器人:名字=「ui.activity.Alert。」 機器人:啓用=‘真’ 機器人:出口=‘真’> ' – EnduroDave
我認爲你是對的,謝謝。但我想問一下,因爲當我想到Alert類時,不是創建了.XML? 出於這個原因,我無法將其包含在Manifiest。 PD(我是新至Android)@Drdavidpier –
@Drdavidpier幫助 –