2016-09-24 16 views
3

アプリが完全に終了したときに、プログラムから通知を送信する方法はありますか?アプリが終了したときに通知を送信

例:ユーザーは、Androidタスクマネージャーでもアプリケーションを終了し、待機します。アプリはX秒後に通知を送信するか、アプリが更新を確認する必要があります。

私はこれらのコード例で作業しようとしましたが:

あなたは、一例でそれを説明しようとすることができた場合は、理由は(​​私のような)初心者この方法で簡単に学ぶことができます。

答えて

1

アクティビティのライフサイクルでこのサービスを開始するだけで、このサービスを使用することができます。このコードでは: startService(new Intent(this, NotificationService.class)); 、あなたは、新しいJavaクラスを作成することができ、その中にこのコードを貼り付けます。

あなただけのmanifest.xmlとサービスを組み合わせる必要がある。この後
public class NotificationService extends Service{ 

Timer timer; 
TimerTask timerTask; 
String TAG = "Timers"; 
int Your_X_SECS = 5; 


@Override 
public IBinder onBind(Intent arg0) 
{ 
    return null; 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId){ 
    Log.e(TAG, "onStartCommand"); 
    super.onStartCommand(intent, flags, startId); 

    startTimer(); 

    return START_STICKY; 
} 


@Override 
public void onCreate(){ 
    Log.e(TAG, "onCreate"); 


} 

@Override 
public void onDestroy(){ 
    Log.e(TAG, "onDestroy"); 
    stoptimertask(); 
    super.onDestroy(); 


} 

//we are going to use a handler to be able to run in our TimerTask 
final Handler handler = new Handler(); 


public void startTimer() { 
    //set a new Timer 
    timer = new Timer(); 

    //initialize the TimerTask's job 
    initializeTimerTask(); 

    //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms 
    timer.schedule(timerTask, 5000, Your_X_SECS*1000); // 
    //timer.schedule(timerTask, 5000,1000); // 
} 

public void stoptimertask() { 
    //stop the timer, if it's not already null 
    if (timer != null) { 
     timer.cancel(); 
     timer = null; 
    } 
} 

public void initializeTimerTask() { 

    timerTask = new TimerTask() { 
     public void run() { 

      //use a handler to run a toast that shows the current timestamp 
      handler.post(new Runnable() { 
       public void run() { 

        //TODO CALL NOTIFICATION FUNC 
        YOURNOTIFICATIONFUNCTION(); 

       } 
      }); 
     } 
    }; 
} 

}

<service 
      android:name=".NotificationService" 
      android:label="@string/app_name" 
      <intent-filter> 
       <action android:name="your.app.domain.NotificationService" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </service> 
+0

ありがとう!私は私のアプリケーションのコードをテストし、それは動作します! – Excel1

+0

[この記事](https://guides.codepath.com/android/Repeating-Periodic-Tasks)によると、 "TimerTask - UIThreadで動作せず、信頼性がありません。 http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/)」 Nikhil Guptaの答えが良いです。 – NargothBond

2

アラームマネージャを使用してこれを行うことができます。 以下の手順に従います。

1)alarmmanagerを使用して、X秒後にアラームを作成します。

Intent intent = new Intent(this, AlarmReceiver.class); 
intent.putExtra("NotificationText", "some text"); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ledgerId, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, 'X seconds in milliseconds', pendingIntent); 

2)お使いのアプリでAlarmBroadCastレシーバーを使用してください。マニフェストファイルの

宣言:

<receiver android:name=".utils.AlarmReceiver"> 
    <intent-filter> 
     <action android:name="android.media.action.DISPLAY_NOTIFICATION" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</receiver> 

3)放送受信機の上で受け取るには、通知を作成することができます。

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // create notification here 
    } 
} 
+0

ご回答いただきありがとうございます。あなたのコードで試してみましたが、すでに動作しています。私は@Vaibhav Kadamのコードを使用することに決めました。私の個人的な使い方がより魅力的だからです。 – Excel1

+0

これはより好ましいアプローチであるようです。 https://guides.codepath.com/android/Starting-Background-Services#using-with-alarmmanager-for-periodic-tasksより詳しい説明があります。 – NargothBond

0

サービスを使用してアクティブなアプリをチェックし、アクティビティが実行されていない場合は通知を表示できます。

関連する問題