我开发了一个在后台运行的应用程序,并且我使用了一个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
...但服务没有启动,我不知道问题所在
请帮帮我!