在后台运行应用程序IntentService

时间:2013-05-01 11:26:01

标签: android service intentservice

我开发了一个在后台运行的应用程序,我使用IntentService开始。

这是我的代码:

public class UsbService extends IntentService {
    /** 
       * A constructor is required, and must call the super IntentService(String)
       * constructor with a name for the worker thread.
       */
      public UsbService() {

          super("UsbService");

      }



      /**
       * The IntentService calls this method from the default worker thread with
       * the intent that started the service. When this method returns, IntentService
       * stops the service, as appropriate.
       */

      @Override
      protected void onHandleIntent(Intent intent) {
          Log.e("why", "fofo");
          Toast.makeText(getApplicationContext(), "starting", Toast.LENGTH_LONG).show();


    //  mNotification.notify(132, builder.build());
          // Normally we would do some work here, like download a file.
          // For our sample, we just sleep for 5 seconds.
        /*  long endTime = System.currentTimeMillis() + 5*1000;
          while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
                  try {
                      wait(endTime - System.currentTimeMillis());
                  } catch (Exception e) {
                  }
              }
          }*/
      }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.servicesusb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app`enter code here`_name"
        android:theme="@style/AppTheme" >

        <service android:name=".UsbService"  >

            </service>
    </application>

</manifest>

我想从ADB shell启动服务,所以我使用了这个命令

am startservice -n com.example.servicesusb/.UsbService

...但是服务没有启动,我不知道问题

请帮帮我!

1 个答案:

答案 0 :(得分:0)

如果您的服务存在,则可以按如下方式启动

adb shell am startservice&lt; packageofservice&gt;

如果仍然无效,请尝试发布运行此命令后收到的错误