2016-05-06 11 views

答えて

0

バックグラウンドサービスを作成する必要があります。あなたも、このサービス中に見ることができます通知が実行されて作成する必要があることに注意してください:

public class BgService extends Service { 
    @Override 
    public int onStartCommand(Intent i, int flags, int startId) { 
     startForeground(C.MAIN_SERVICE_NOTIFICATION_ID, buildNotification(getString(R.string.service_title))); 
     return START_NOT_STICKY; 
    } 

    protected Notification buildNotification(String content) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setTicker(getString(R.string.app_name)) 
       .setContentTitle(getString(R.string.app_name)) 
       .setContentText(content) 
       .setSmallIcon(R.drawable.notification_icon) 
       .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon)) 
       .setWhen(System.currentTimeMillis()) 
       .setAutoCancel(false) 
       .setOngoing(true) 
       .setContentIntent(pendingIntent); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
      builder.setPriority(Notification.PRIORITY_HIGH); 

     Notification notification = builder.build(); 
     notification.flags |= Notification.FLAG_NO_CLEAR; 
     return notification; 
    } 
} 

あなたのアプリケーションからこのサービスを開始することができます

public class MyApp extends Application { 
    @Override 
    public void onCreate() { 
     startService(new Intent(this, BgService.class)); 
    } 
} 
関連する問題