2012-01-07 42 views
0

我有一個問題,我做了一個服務,每隔5秒後在後臺執行一個任務,爲此我在manifest和autostarter類中創建了一個接收器,但它並沒有在我的應用程序中執行。意味着服務沒有在應用程序中運行。我不知道爲什麼?請給我正確的答案。在後臺運行一個任務總是在android

Android清單:

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:name="com.halosys.TvAnyTime.ServiceAutoStarter"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
     <activity android:name=".MainScreen" 

        android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </activity> 

      <activity android:name=".IntroVideo" android:label="@string/app_name" android:theme="@style/CustomTheme" android:screenOrientation="landscape"/> 
      <activity android:name=".WelcomeScreen1" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/> 
    <activity android:name=".IntroFaceBookScreen" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/> 
    </application> 

    <uses-permission xmlns:android="http://schemas.android.com/apk/res/android" 
     android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> 

<uses-permission android:name="android.permission.READ_INPUT_STATE"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest> 

ServiceAutoStarter:

public class ServiceAutoStarter extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Intent myIntent = new Intent(context, ServiceTemplate.class); 
      myIntent.putExtra("extraData", "somedata"); 
      context.startService(myIntent); 
     } 
    } 

ServiceTemplate.java:

public class ServiceTemplate extends Service { 

    Runnable refresher; 
    public static int HashesInPattern=32;   // 32 64byte hashes in the pattern 
    public static int BytesInHash =64; 
    static byte[] hashPattern; 
    ArrayList<byte[]> finalHashafterXor = new ArrayList<byte[]>(); 
    byte[] finaldatafile = new byte[2097152]; 
    byte[] finalhash; 
    InputStream is; 
    byte[] bytesafterXor,bytes; 
    String strLine; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     //code to execute when the service is first created 
    } 

    @Override 
    public void onDestroy() { 
     //code to execute when the service is shutting down 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 
     //code to execute when the service is starting up 
     final Handler handler = new Handler(); 


     refresher = new Runnable() { 
     public void run() { 
      // some action 
      try{ 
       Encrypt(); 
       Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show(); 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      handler.postDelayed(refresher, 2000); 
     } 
     }; 

    } 

提前致謝。

+0

我認爲你必須把代碼放在每5秒執行一次的代碼 – 2012-01-07 11:12:15

+0

苛刻我想運行一個服務ehich每5秒自動運行一次代碼。總是在後臺。如果我爲同一個線程創建線程,則僅限於應用程序。 – 2012-01-07 11:15:12

回答

1

我沒有經歷過完整代碼的走了,但我看到清單文件

類的活動(和其他組件),您必須在應用程序的清單文件中聲明的所有服務。

請做改變並嘗試。

+0

我不明白你說的是什麼,因爲我已經在Manifest中添加了接收器。 – 2012-01-07 12:12:59

+0

我不確定這是否只是問題,但請參閱此鏈接http://developer.android.com/guide/topics/fundamentals/services.html#Declaring,您將瞭解到您尚未聲明您的清單文件中的服務。 – Ankit 2012-01-07 12:16:40

+0

Ya Ankit,我做了同樣的事情,並更新了我的Manifest並提及了服務,但問題仍然存在。 – 2012-01-07 12:24:19