Reputation: 19288
I'm trying to run a thread by starting it with a service, but it won't start. It won't even log.
This is my inner service class. The service class creates a new background thread and starts it. The backgroundthread and the service class are both innerclasses so they can reach the variables.
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;
}
}
I'm starting the service in the MainActivity by this:
startService(new Intent(this, MyService.class));
And this is my manifest:
<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>
Please help me out
Upvotes: 0
Views: 870
Reputation: 101
try this code in your start service button, I hope it works :
Intent intent = new Intent(MainActivity.this,Service.class);
MainActivity.this.startService(intent);
Upvotes: 0
Reputation: 1007494
This is my inner service class
That is not possible. It either needs to be a static inner class (in which case, your <service>
element also needs changing) or it needs to be a separate public class.
The backgroundthread and the service class are both innerclasses so they can reach the variables.
I do not see any "variables" that your service needs to "reach". Moreover, it is impossible for Android to create an instance of an inner class, since it does not have an instance of the outer class.
Upvotes: 1