2017-04-03 17 views
-3

UIから独立したAndroidサービスを使用して通知を作成しています。これは完全に正常に動作します。以下はコードです。Androidサービスを30秒ごとに実行

​​

このサービスもシステム起動時に自動的に開始されます。 CheckNewEntryは、データベースをチェックし、変更があれば通知を送信するMy AsyncTaskです。私はCheckNewEntryを追加していないので、この質問の範囲を超えています。

ここでは、30秒または1分ごとにCheckNewEntryを実行します。

誰でも手助けできますか?

+0

Handlerメソッドを使用 –

+0

私はAndroid開発の初心者ですので、コードを修正してください。私はそうしようとしているが失敗している。 –

+0

http://stackoverflow.com/a/13423036/3395198 –

答えて

0

異なるStackoverflowの質問/回答を経た後、私は自分の解決策を思いついた。

以下は私が作成して今作業しているコードです。

public class SendNotificationService extends Service { 
    public Context context = this; 
    public Handler handler = null; 
    public static Runnable runnable = null; 

    @Override 
    public void onCreate() { 
     handler = new Handler(); 
     runnable = new Runnable() { 
      public void run() { 
       String requested_method = "LoadBU"; 
       String bu_status = "1"; 

       CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this); 
       checkNewEntry.execute(requested_method, bu_status); 

       handler.postDelayed(runnable, 10000); 
      } 
     }; 

     handler.postDelayed(runnable, 15000); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     return START_STICKY; 
    } 

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

    @Override 
    public void onDestroy() { 
     handler.removeCallbacks(runnable); 

     Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show(); 
    } 
} 

誰かがより良い解決策を提供できる場合は、投稿してください。

0

このようなハンドラーを使用できます。

public class SendNotificationService extends Service { 
     Context context; 
     String test_heading; 
     String test_body; 
     public static Runnable runn; 
     public static Handler hand =new Handler(); 
     final class notifThread implements Runnable { 
      int service_id; 

      notifThread(int service_id) { 
       this.service_id = service_id; 
      } 

      @Override 
      public void run() { 
       String requested_method = "LoadBU"; 
       String bu_status = "1"; 

       CheckNewEntry checkNewEntry = new CheckNewEntry(SendNotificationService.this); 

    runn = new Runnable() { 
       @Override 
       public void run() { 

        checkNewEntry.execute(requested_method, bu_status); 

        hand.postDelayed(runn, 30000); 
       } 
      }; 


      runn.run(); 


      stopSelf(this.service_id); 
     } 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Thread thread = new Thread(new notifThread(startId)); 
     thread.start(); 

     return START_STICKY; 
    } 

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

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     Toast.makeText(this, "Notifications Stopped...", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

私は今それを試してみるつもりです。 –

+0

タスクを実行できません:タスクはすでに実行されています(タスクは1回だけ実行できます) –

+0

私のコードはあまり変更されていないので、私はあなたの答えを優先しました。しかし、私が自分自身を考え出した答えも掲載されています。 –

関連する問題