Java 如何使用NotificationCompat.Builder获取通知计数
我正在开发一个android应用程序,其中一个功能是提醒用户,如果他超过了允许的速度,我想向用户发送三个通知通知。。。我找到了一些如何计算notification.number的示例,但我正在使用Java 如何使用NotificationCompat.Builder获取通知计数,java,android,performance,notifications,android-sharedpreferences,Java,Android,Performance,Notifications,Android Sharedpreferences,我正在开发一个android应用程序,其中一个功能是提醒用户,如果他超过了允许的速度,我想向用户发送三个通知通知。。。我找到了一些如何计算notification.number的示例,但我正在使用 NotificationCompat.Builder 这是我的密码 public class SpeedService extends IntentService { CharSequence appName ; int count ; MyReceiver myRecei
NotificationCompat.Builder
这是我的密码
public class SpeedService extends IntentService {
CharSequence appName ;
int count ;
MyReceiver myReceiver;
int speed ;
int allowedSpeed ;
public SpeedService() {
super("SpeedService");
}
@Override
protected void onHandleIntent(Intent intent) {
appName = getString(R.string.app_name);
allowedSpeed = APP.getInstance().getSharedPreferences().readInt("streetSpeed");
Log.e("streetSpeed" , allowedSpeed+"");
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(GPSService.MY_ACTION);
this.registerReceiver(myReceiver, intentFilter);
if(allowedSpeed <= speed && count ==0 ){
createNotification(R.raw.alert1, "");
}
if(allowedSpeed <= speed && count ==1){
createNotification(R.raw.alert2, "");
Log.e("NotificationCount" , count+"");
}
if(allowedSpeed <= speed && count ==2){
createNotification(R.raw.alert3, "");
}
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
speed = arg1.getIntExtra("speed", 0);
}
}
private void createNotification(int raw , String message){
int pendingNotificationsCount = APP.getPendingNotificationsCount() + 1;
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + raw);
Random random = new Random();
int ID = random.nextInt(9999 - 1000) + 1000;
Intent intent=new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("data", message);
intent.putExtra("KEY", "YOUR VAL");
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle(appName)
.setContentText(message)
.setSmallIcon(R.drawable.marker_icon)
.setContentIntent(pendingIntent).setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setNumber(pendingNotificationsCount).setContentText(message);
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(ID,builder.build());
MediaPlayer mp= MediaPlayer.create(this, raw);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setLooping(false);
mp.start();
pendingNotificationsCount ++;
APP.setPendingNotificationsCount(pendingNotificationsCount);
}
}
公共类SpeedService扩展了IntentService{
字符序列appName;
整数计数;
我的接收器我的接收器;
整数速度;
允许的速度;
公共特快专递服务{
超级(“特快专递”);
}
@凌驾
受保护的手部内容无效(意图){
appName=getString(R.string.app\u name);
allowedSpeed=APP.getInstance().getSharedReferences().readInt(“streetSpeed”);
Log.e(“streetSpeed”,allowedSpeed+”);
myReceiver=新的myReceiver();
IntentFilter IntentFilter=新的IntentFilter();
intentFilter.addAction(GPSService.MY_操作);
此注册表接收程序(myReceiver、intentFilter);
如果(允许的)速度