我有這樣的服務延伸IntentService。清晰度上IntentService
public class RefreshService extends IntentService {
static final String TAG = "RefreshService";
public RefreshService() {
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreated");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroyed");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final String username = prefs.getString("username", "").trim();
final String password = prefs.getString("password", "").trim();
Log.d(TAG, "onDestroyeds");
if (TextUtils.isEmpty(username) || (TextUtils.isEmpty(password))){
Toast.makeText(this, "Please update your username and password", Toast.LENGTH_LONG).show();
return;
}
// here code for fetching data and inserting into db.
}
}
當設置丟失,而不是舉杯消息是表演,我碰到下面的錯誤。
07-12 18:28:33.125 2961-2961/com.example.krishna.yamba I/menu_item_selected﹕ [0,Refresh]
07-12 18:28:33.143 2961-2961/com.example.krishna.yamba D/RefreshService﹕ onCreated
07-12 18:28:33.147 2961-3637/com.example.krishna.yamba D/RefreshService﹕ onDestroyeds
07-12 18:28:33.147 2961-3637/com.example.krishna.yamba D/RefreshService﹕ onDestroyeds
07-12 18:28:33.181 2961-2961/com.example.krishna.yamba D/RefreshService﹕ onDestroyed
07-12 18:28:33.197 2961-3637/com.example.krishna.yamba W/MessageQueue﹕ Handler (android.view.ViewRootImpl$ViewRootHandler) {38f6505f} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.view.ViewRootImpl$ViewRootHandler) {38f6505f} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.view.ViewRootImpl.loadSystemProperties(ViewRootImpl.java:5413)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:378)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:253)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.widget.Toast$TN.handleShow(Toast.java:414)
at android.widget.Toast$TN$1.run(Toast.java:322)
at android.os.Handler.handleCallback(Handler.java:738)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
我假設以下
- 在onHandleIntent方法中的代碼是在不同的線程運行都是如此。
- 因此,在這裏做一些密集的任務是很好的。 (在這種情況下,我只是提取和更新db)
當設置正確時,一切正常。然後把取消和更新敬酒信息不應該是一個問題吧?
如何把敬酒消息時,設置爲空白?
,以及如何使用該服務什麼時候?它是否只在應用程序內部的用戶啓動時運行?還是可以在後臺任意啓動(例如通過AlarmManager)? – Karakuri
實際上都是兩種方式 –