我是android开发新手, 我正在创建一个应用程序,您可以在其中标记您的出勤率, 因此,当出勤率低时,它会发送通知, 我写下面的代码
private void setup(final String keys, final int uid, final String x) {
final int[] uid0 = {uid};
br = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
NotificationCompat.Builder notf;
uid0[0] += 1;
notf = new NotificationCompat.Builder(see.this);
notf.setAutoCancel(true);
notf.setSmallIcon(R.mipmap.ic_launcher);
notf.setTicker("Your attendance is low!!");
notf.setWhen(System.currentTimeMillis());
notf.setContentTitle("Your "+keys+" attendance is low !!");
notf.setContentText("It is "+x+" %");
Intent inte = new Intent(see.this,see.class);
PendingIntent penin= PendingIntent.getActivity(see.this,0,inte,PendingIntent.FLAG_UPDATE_CURRENT);
notf.setContentIntent(penin);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uid,notf.build());
}
};
registerReceiver(br, new IntentFilter("com.ajaypilaniya.omg"));
pi = PendingIntent.getBroadcast( this, 0, new Intent("com.ajaypilaniya.omg"),
0 );
am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
这是定义通知属性的设置功能。 在课堂上我有这些行
final long ONE_SECOND = 1000;
final long TWENTY_SECONDS = ONE_SECOND * 10;
PendingIntent pi;
BroadcastReceiver br;
AlarmManager am;
setup(keys,uid,kk);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
TWENTY_SECONDS, pi);
在android清单中我添加了这些行
<user-permission android:name="android.permission.WAKE_LOCK"/>
<user-permission android:name="com.android.alarm.permission.SET_ALARM"/>`
但是当我关闭应用程序时,它在10秒后没有显示通知,那么怎么做呢? 在某处我读到我们需要在清单中为广播接收器添加代码,但我不知道该怎么做?