2011-12-24 163 views
7

我有一個簡單的AutoStart應用程序與TimerTask實現,這在幾乎很多設備上都能正常工作。問題在於它不在Samsung Galaxy Y(2.3.6)DELL XCD35(2.2)中工作。當設備啓動TimerTask工作幾秒鐘,然後關閉。我檢查了Application->Manage Application,我看到Applcation已經在Force Stop狀態。這意味着一些如何我的應用程序在幾秒鐘後停止。那麼,這兩個設備中的weird behaviour的原因是什麼,如果任何人有解決方案,請分享它。AutoStart應用程序無法正常工作

以下是我的代碼。

MyReceiver.java

public class MyReceiver extends BroadcastReceiver{ 

    private Timer mTimer = new Timer(); 
    @Override 
    public void onReceive(Context context, Intent arg1) { 
     Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show(); 
     Log.d("TAG","Device Booted"); 
     mTimer.scheduleAtFixedRate(new MyTimerTask(), 2000,2000); 
    } 

    private class MyTimerTask extends TimerTask 
    { 
     @Override 
     public void run() { 
      Log.d("TAG","TimerTask executed...."); 
     } 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.autostart.app" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <receiver android:name=".MyReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 
+0

啊!任何關於投票的理由? – 2011-12-24 11:43:00

+3

一個有效的,格式良好的問題,不需要反對投票... – WarrenFaith 2011-12-24 11:54:08

回答

0

我覺得在一些AndroidOS有時OS殺死正在運行的線程,而DevicesBoots的Android不熟悉或不承認它。這就是爲什麼TimerTask在某些設備上工作的原因,在某些設備上運行5-10秒,然後是Android操作系統上的應用程序ForceStoppedautomaticallyDeviceBoot注 - 它的強制從管理應用程序停止,而不是強制關閉,所以我沒有在Logcat中得到任何錯誤)。

因此,在這種情況下,解決辦法是使用inbuiltMechanismAndroidOS識別並不能殺死它,保持它的運行模式。在這種情況下,我管理使用AlarmManager執行我的任務,它的工作原理。

我可能不是正確的,但我的最終解決方案是使用AlarmManager使我的應用程序在每個設備上工作。

@Override 
    public void onReceive(Context context, Intent arg1) { 

     Intent myIntent = new Intent(context, AlarmService.class); 
     PendingIntent pendingIntent = PendingIntent. 
             getService(context, 0, myIntent, 0); 
     AlarmManager alarmManager = (AlarmManager) context 
            .getSystemService(Context.ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
         System.currentTimeMillis() + 2000, 2000, pendingIntent); 
    } 

UPDATE:

AlaramManager is critical system service that runs all the time.

2

我會建議你使用AlarmManager而不是TimerTask的,因爲我面對你的許多設備中描述了同樣的問題。

public void onReceive(Context context, Intent arg1) { 
    Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show(); 
    Log.d("TAG","Device Booted"); 
AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(); 
    intent.setAction("ALARM_MANAGER_ACTION");//can add any string action here 
    PendingIntent pi = PendingIntent.getBroadcast(mContext 
             .getApplicationContext(), 0, intent,0); 
AM.set(AlarmManager.RTC,selectedTime, pi); 
AM.setRepeating(AM.RTC_WAKEUP, System.currentTimeMillis()+2000, 2000, pi); 
    } 


    public class MyReceiver1 extends BroadcastReceiver{ 
    //event will come here 
    private Timer mTimer = new Timer(); 
    @Override 
    public void onReceive(Context context, Intent arg1) { 
// check if event is same as you broadcasted through alarmManager 
    Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show(); 
    Log.d("TAG","TimerTask executed...."); 

} 

在你的應用程序中添加廣播接收器,這應該聽("ALARM_MANAGER_ACTION")行動。並將此操作添加到清單文件中。 我敢打賭,它也可以在這兩個設備上工作。

相關問題