我正在尝试使用AlarmManager和BroadcastReceiver创建通知。 (API 19,KitkKat)
我正在按照步骤教程https://www.youtube.com/watch?v=gm5n_hRIR-c (从10:51开始)
但我无法让应用程序运行。没有投掷通知。 :/ 帮助,这是我的代码。
我的主要活动
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());
}
}
Mi 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>
答案 0 :(得分:1)
您没有在清单中注册广播。这就是它无法正常工作的原因。广播必须在使用前注册。因此,首先在清单中注册您的类警报,如果您正在使用任何操作,那么还要使用意图过滤器在清单文件中声明操作。