如何使用NotificationCompat.Builder获取通知计数

时间:2017-03-04 23:44:53

标签: java android performance notifications android-sharedpreferences

我正在开发一个Android应用程序,它的一个功能是提醒用户,如果他超过了允许的速度,我想向用户发送三个警报通知...我找到了一些如何计算通知的例子。号码,但我正在使用

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);



    }
}

任何帮助请...

0 个答案:

没有答案