2012-01-07 38 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