始终在android中运行后台任务

时间:2012-01-07 11:00:28

标签: android

我遇到一个问题,我提出了一个服务,它在每5秒后在后台执行一个任务,为此我在manifest和autostarter类中创建了一个接收器,但它在我的应用程序中没有被执行。表示该服务未在应用程序中运行。我不知道为什么?请建议我找到正确的答案。

Android Manifest:

<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);
         }
       };

   }

提前致谢。

1 个答案:

答案 0 :(得分:1)

我没有查看整个代码,但我看到了清单文件

与活动(和其他组件)类似,您必须在应用程序的清单文件中声明所有服务。

请更改并尝试。