【问题标题】:Foreground service example [duplicate]前台服务示例 [重复]
【发布时间】:2014-03-21 20:06:12
【问题描述】:

我是安卓新手。任何人都可以发布“带有通知的android前台服务示例”的链接或代码。我用谷歌搜索,但没有得到任何前台服务示例。

【问题讨论】:

    标签: android android-layout android-intent android-fragments


    【解决方案1】:

    创建一个Notification,可能使用Notification.BuilderNotificationCompat.Builder,并将其传递给服务上的startForeground()

    public class Downloader extends IntentService {
      private static int FOREGROUND_ID=1338;
    
      public Downloader() {
        super("Downloader");
      }
    
      @Override
      public void onHandleIntent(Intent i) {
        startForeground(FOREGROUND_ID,
                        buildForegroundNotification(filename));
    
        // do useful work here
    
        stopForeground(true);
      }
    
      private Notification buildForegroundNotification(String filename) {
        NotificationCompat.Builder b=new NotificationCompat.Builder(this);
    
        b.setOngoing(true);
    
        b.setContentTitle(getString(R.string.downloading))
         .setContentText(filename)
         .setSmallIcon(android.R.drawable.stat_sys_download)
         .setTicker(getString(R.string.downloading));
    
        return(b.build());
      }
    }
    

    (来自this sample project 的精简服务)

    【讨论】:

      【解决方案2】:

      一个更好的例子是:

      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
      if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
      Log.i(LOG_TAG, "Received Start Foreground Intent ");
      Intent notificationIntent = new Intent(this, MainActivity.class);
      notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
      | Intent.FLAG_ACTIVITY_CLEAR_TASK);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
      notificationIntent, 0);
      
      Intent previousIntent = new Intent(this, ForegroundService.class);
      previousIntent.setAction(Constants.ACTION.PREV_ACTION);
      PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
      previousIntent, 0);
      
      Intent playIntent = new Intent(this, ForegroundService.class);
      playIntent.setAction(Constants.ACTION.PLAY_ACTION);
      PendingIntent pplayIntent = PendingIntent.getService(this, 0,
      playIntent, 0);
      
      Intent nextIntent = new Intent(this, ForegroundService.class);
      nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
      PendingIntent pnextIntent = PendingIntent.getService(this, 0,
      nextIntent, 0);
      
      Bitmap icon = BitmapFactory.decodeResource(getResources(),
      R.drawable.truiton_short);
      
      Notification notification = new NotificationCompat.Builder(this)
      .setContentTitle("Truiton Music Player")
      .setTicker("Truiton Music Player")
      .setContentText("My Music")
      .setSmallIcon(R.drawable.ic_launcher)
      .setLargeIcon(
      Bitmap.createScaledBitmap(icon, 128, 128, false))
      .setContentIntent(pendingIntent)
      .setOngoing(true)
      .addAction(android.R.drawable.ic_media_previous,
      "Previous", ppreviousIntent)
      .addAction(android.R.drawable.ic_media_play, "Play",
      pplayIntent)
      .addAction(android.R.drawable.ic_media_next, "Next",
      pnextIntent).build();
      startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
      notification);
      } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
      Log.i(LOG_TAG, "Clicked Previous");
      } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
      Log.i(LOG_TAG, "Clicked Play");
      } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
      Log.i(LOG_TAG, "Clicked Next");
      } else if (intent.getAction().equals(
      Constants.ACTION.STOPFOREGROUND_ACTION)) {
      Log.i(LOG_TAG, "Received Stop Foreground Intent");
      stopForeground(true);
      stopSelf();
      }
      return START_STICKY;
      }
      

      参考tutorial here.. 可能对你有帮助

      【讨论】:

        猜你喜欢
        • 2019-11-13
        • 1970-01-01
        • 2022-01-19
        • 2018-10-30
        • 2018-08-03
        • 2017-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-09-26
        相关资源
        最近更新 更多