2015-02-10 78 views
0

我想以固定的時間間隔從服務器獲取通知,因此我已將服務器響應設置爲服務。所以,基本上我使用服務來運行後臺並創建通知。我的問題是我收到的通知不是固定的時間間隔。那麼如何以固定的時間間隔獲取通知。即使重新啓動手機,我也想繼續我的服務(獲取通知)。我有點困惑,如果手機將被關閉,那麼我不得不重新啓動我的服務。如果我必須重新啓動它,那我該如何處理。您的幫助將不勝感激。提前致謝。通知和服務在Android

Alert_notifications.java 
public class Alert_notifications extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_alert_notifications); 
     Button buttonStartService = (Button)findViewById(R.id.startservice); 
     Button buttonStopService = (Button)findViewById(R.id.stopservice); 

     buttonStartService.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(Alert_notifications.this, com.example.gpstracking.NotifyService.class); 
       Alert_notifications.this.startService(intent); 
      }}); 

     buttonStopService.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(); 
       intent.setAction(NotifyService.ACTION); 
       intent.putExtra("RQS", NotifyService.STOP_SERVICE); 
       sendBroadcast(intent); 
      }}); 

    } 
} 



NotifyService.java 


public class NotifyService extends Service { 

    final static String ACTION = "NotifyServiceAction"; 
    final static String STOP_SERVICE = ""; 
    final static int RQS_STOP_SERVICE = 1; 
    private static final String url_Weather_details1="http://198.168.1.2/Weatherforecast1/"; 
    private static final String TAG_SUCCESS = "success"; 


    NotifyServiceReceiver notifyServiceReceiver; 

    private static final int MY_NOTIFICATION_ID = 1; 
    private NotificationManager notificationManager; 
    private Notification myNotification; 
    private final String myBlog = "http://android-er.blogspot.com/"; 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     notifyServiceReceiver = new NotifyServiceReceiver(); 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 
     JSONParser jsonParser = new JSONParser(); 
     params.add(new BasicNameValuePair("LAT", "LAT")); 
     params.add(new BasicNameValuePair("LONGITUDE", "LONG")); 
     Log.d("params", params.toString()); 
     // getting weather details by making HTTP request 
     // Note that weather details url will use GET request 
     JSONObject json = jsonParser.makeHttpRequest(url_Weather_details1, 
       "GET", params); 
     // check your log for json response 
     Log.d("Weather Details", json.toString()); 

     // json success tag 
     int success = 0; 
     try { 
      success = json.getInt(TAG_SUCCESS); 
      System.out.println("success"+success); 
     } 
     catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     if (success == 2) { 
      // successfully received weather details 

     // TODO Auto-generated method stub 

     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(ACTION); 
     registerReceiver(notifyServiceReceiver, intentFilter); 

     // Send Notification 
     notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     myNotification = new Notification(R.drawable.ic_launcher,"Notification!", System.currentTimeMillis()); 

     Context context = getApplicationContext(); 
     String notificationTitle = "Heavy Rain!"; 
     String notificationText = ""; 
     Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog)); 
     PendingIntent pendingIntent = PendingIntent.getActivity(
       getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
     myNotification.defaults |= Notification.DEFAULT_SOUND; 
     myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
     myNotification.setLatestEventInfo(context, notificationTitle, 
       notificationText, pendingIntent); 
     notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 

     } 
     return super.onStartCommand(intent, flags, startId); 

    } 


    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     this.unregisterReceiver(notifyServiceReceiver); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public class NotifyServiceReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      // TODO Auto-generated method stub 
      int rqs = arg1.getIntExtra("RQS", 0); 
      if (rqs == RQS_STOP_SERVICE) { 
       stopSelf(); 
      } 
     } 
    } 

} 
+0

如果您正在從服務器發出通知,則可以爲其設置cronjob並設置/計劃時間間隔。請讓我知道,如果這個想法有幫助,或者你需要更多的澄清。 – sUndeep 2015-02-10 09:08:36

回答

0

我會建議使用GCM提供「推」 -notifications到Android客戶端或客戶端可以交替做網絡電話,檢查是否有新的通知。 要注意設備在服務工作期間沒有入睡,您還應該查看WakefulBroadcastReceiver或獲取WakeLock。如果您有興趣在啓動後重新啓動服務,請檢查此answer以接收BroadcastReceiver上的ACTION_BOOT_COMPLETED系統事件,您可以在其中重新啓動服務。