2011-11-16 70 views
0

你好我已經寫了服務onstart()提醒代碼,當用戶插入日期時間和插入記錄在那個時候服務調用startservice()函數,但只有服務正在開始,當我插入記錄即我得到提醒時它會從我的活動中接到電話,但我想要在三天之後提醒,所以我應該如何始終保持服務,以便將來可以提醒?或者我應該如何使服務連接保持活動?我應該從我的任何活動或什麼調用bindservice()frunction? 在此先感謝---如何讓我的應用程序服務始終在android2.1中運行?

回答

0

首先沒有必要的服務,您可以使用在特定的時間和日期安排的活動和節目警報AlarManagerClass Link Alarmanger類。如果您想要在很長時間後顯示消息,則可以通過在相關時間點啓動服務的AlarmManager安排一個待定意圖來執行此操作。完成後,按照以上答案的說明再次停用該服務。另外,您可以將數據永久存儲到共享首選項中。您可以在任何時候檢索它,以便在設備重新啓動時重新安裝或用於其他目的。

+0

如何在完成當前服務任務後重新啓動()服務,以便將來發送消息? – Sam

+0

有可能從braodcast reciever對reboot.Check這個http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ –

+0

您可以再次綁定,然後啓動服務啓動服務。 –

2

不要讓您的服務始終運行。不需要時,它會消耗電池和內存¹。 寧可安排PendingIntent通過AlarmManager即在相關時間點開始服務以完成工作。完成後,再次終止服務。

一般來說,androids服務在「普通」計算機上使用不同的服務/守護進程。他們有一個任務,他們執行,然後他們退出(通常通過Service.stopSelf(),直到有人再次啓動他們做更多的工作。

這裏是一個小例子的AlarmManager如何使用:

// get a calendar with the current time 
Calendar cal = Calendar.getInstance(); 
// add 15 minutes to the calendar object 
cal.add(Calendar.MINUTE, 15); 

Intent intent = new Intent(ctx, YourService.class); 
PendingIntent pi = PendingIntent.getService(this, 123, intent, 
              PendingIntent.FLAG_UPDATE_CURRENT); 

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi); 

這將啓動意圖從現在開始YourService在15分鐘內。有很多文件用這種方式發送意圖,搜索一下。

¹這最終會讓您的用戶感到沮喪:「爲什麼此應用會浪費我的電池?」是一個很常見的問題

0

有時候,您的android應用程序可能有必要在將來某個時候完成任務。爲了做到這一點,你必須安排一個活動(也可以是服務),使用Android的AlarmManager運行。這篇文章將顯示:

* How to set up a receiver for the scheduled event 
* How to create an activity from this receiver 
* Using the AlarmManager and the created classes to successfully receive and process a scheduled event 

創建一個BroadcastReceiver

你需要的第一件事就是接收器,接收該事件。接收器有幾個關鍵方面可以正常工作。首先創建一個擴展BroadcastReceiver的類並覆蓋並實現必要的onReceive(Context context,Intent intent)方法。下面是使用吐司消息一個基本的例子:

package com.justcallmebrian.alarmexample; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 
import android.os.Bundle; 

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    try { 
    Bundle bundle = intent.getExtras(); 
    String message = bundle.getString("alarm_message"); 
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
    Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
    e.printStackTrace(); 

    } 
} 

} 

在前面的例子中,我們只是簡單地打印出由alarm_message名下傳入的字符串提供的消息。對於那些不熟悉吐司的人來說,它基本上是給用戶的簡短而快速的信息。在這裏你可以找到更多有關Toast的信息。

除了實際創建接收事件的類外,還必須在AndroidManifest.xml中聲明它。以下是應用程序標記關閉之前應添加的行(之前)。

這基本上指出,類AlarmReceiver可用,並開始私人過程。一旦完成,您的BroadcastReceiver即可開始使用。

使用AlarmManager設置事件 爲了接收事件,您顯然必須安排事件。有三種調度事件的方式(使用set方法的一次事件,使用setRepeating方法的重複事件並最終使用setInexactRepeating)。本教程將介紹使用set方法的一次性警報。有關其他事件的更多信息,您可以查看AlarmManager。

下面的代碼片段將得到AlarmManager並設置一個事件,從當前時間發生5分鐘:

// get a Calendar object with current time 
Calendar cal = Calendar.getInstance(); 
// add 5 minutes to the calendar object 
cal.add(Calendar.MINUTE, 5); 
Intent intent = new Intent(ctx, AlarmReceiver.class); 
intent.putExtra("alarm_message", "O'Doyle Rules!"); 
// In reality, you would want to have a static variable for the request code instead of 192837 
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

// Get the AlarmManager service 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 

這段代碼基本上獲得新的日曆對象,並將5分鐘吧。 Intent是使用我們之前創建的AlarmReceiver創建的。更重要的代碼是設置FLAG_UPDATE_CURRENT標誌。如果沒有這個標誌,那麼作爲附加信息傳遞的信息將會丟失,並且不會被接收器獲得。

有了這些代碼段,你應該能夠在廣播接收器來創建和運行一些任務。但是,有時您可能希望在警報事件中啓動新的活動(或服務)。爲此,您需要讓AlarmReceiver創建並啓動新的活動。

起價的BroadcastReceiver 啓動接收器內的活動的活動具有被需要額外的標誌。我們將改變以往的onReceive爲AlarmReceiver來完成這件事:

@Override 
public void onReceive(Context context, Intent intent) { 
try { 
Bundle bundle = intent.getExtras(); 
String message = bundle.getString("alarm_message"); 

Intent newIntent = new Intent(context, AlarmActivity.class); 
newIntent.putExtra("alarm_message", message); 
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(newIntent); 
} catch (Exception e) { 
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); 
e.printStackTrace(); 

} 
} 

現在你只需要創建新的AlarmActivity,你會做任何其他活動。不要忘記在AndroidManifest.xml文件中包含新創建的活動。

+0

喜雅您的權利,但實際上我想去做發送短信的一些任務future.so爲保持繼續運行(),它的任務,我呼籲來自OnStart中發送短信的活動,也寫在OnStart中這一任務的代碼()。但是當我插入的記錄和callng startservice提交按鈕後,那麼只有服務獲取調用,但我想保持服務活着,生病需要的時候摧毀,所以我應該怎麼辦呢?我應該從活動綁定服務? – Sam

相關問題