如何在暂停时将通知时间更改为秒表应用程序



我想在应用程序处于暂停停止状态时将我的通知时间与秒表同步。

final NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
    .setOngoing(StartRun)
    .setContentText( Lap + " " + s + " " ) 
    .setAutoCancel(true)
    .setLargeIcon(BitmapFactory.decodeResour‌​ce(getResources(), R.drawable.a)) 
    .setSmallIcon(R.drawable.a); 
mBuilder.setContentIntent(pendingIntent);
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotifyMgr.notify(mNotificationId,mBuilder.build());

是你的应用的秒表还是其他应用的秒表?

如果秒表属于您自己的应用程序,则只需在服务上运行它并在那里执行通知业务即可。尽管如果您的应用的生命周期处于 onDestroy,这将不起作用。但是,您不需要它在活动销毁后运行,因为很可能秒表本身不存在。

如果秒表属于另一个应用程序,或者它是您自己的应用程序,但在与您的活动分离的 backGround 上运行,则需要确保您的服务的生命周期也未附加到启动它的活动。

如果 stopWatch 属于另一个应用程序,请在清单中为您的服务注册 BroadCastListener。

        public class MyService extends Service {
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        SharedPreferences preferences=getSharedPreferences("Timer",MODE_PRIVATE);
        SharedPreferences preferences1=getSharedPreferences("Lap",MODE_PRIVATE);
        seconds=preferences.getInt("time",0);
         hours=seconds/3600;
        String Lap=preferences1.getString("Lap","null");
         minutes=(seconds%3600)/60;
         sec=seconds%60;
        StartRun=true;
        final int mNotificationId = 001;
  /* Creates an explicit intent for an Activity in your app */
        Intent i = getPackageManager()
                .getLaunchIntentForPackage(getPackageName())
                .setPackage(null)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
        String s=("" + hours + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", sec));
        final NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setOngoing(StartRun)
                        .setContentText( Lap + "  " + s + " " )
                        .setAutoCancel(true)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.a))
                        .setSmallIcon(R.drawable.a);
                        mBuilder.setContentIntent(pendingIntent);
                    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    mNotifyMgr.notify(mNotificationId,mBuilder.build());
        return START_STICKY;
    }
    NotificationManager mNotifyMgr;
}

最新更新