我正在尝试通过使用服务启动线程来运行线程,但它不会启动。它甚至不会记录。
这是我的内部服务类。服务类创建一个新的后台线程并启动它。backgroundthread 和服务类都是内部类,因此它们可以访问变量。
public class MyService extends Service {
private BackgroundThread background;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
background = new BackgroundThread();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
background.start();
return startId;
}
}
我通过以下方式在 MainActivity 中启动服务:
startService(new Intent(this, MyService.class));
这是我的清单:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.antioversleepapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.antioversleepapp.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<activity
android:name="com.example.antioversleepapp.InfoActivity"
android:label="@string/title_activity_info" >
</activity>
<service android:enabled="true" android:name="com.example.antioversleepapp.MyService" />
</application>
请帮帮我