IAmTheSquidward
IAmTheSquidward

Reputation: 582

Service not starting when button is pressed?

I know this has been asked a million times before, but nothing is working for me. I have a service in a separate class that needs to be started when a button is pushed, after an application is launched from a LaunchIntent.

Long story short, here's my goal:

run commands>wait three seconds for commands to run>launch app>start service

The service is to monitor for the CONFIGURATION_CHANGED broadcast.

Manifest (the parts that matter):

</activity>

    <receiver android:name="MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.CONFIGURATION_CHANGED" >
                </action>
        </intent-filter>
    </receiver>

    <service android:enabled="true" android:name=".MyService" />

</application>

</manifest>

MyService.java:

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.app.Service;

public class MyService extends Service {

String[] commandsdefault = {"/x"};

public void onCreate() {
    Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

public void onDestroy() {
    Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

public void onReceive(Context context, Intent intent) {
    MainActivity ogres = new MainActivity();
    ogres.RunAsRoot(commandsdefault);
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

And then I simply have the following line in my MainActivity.java to call the service to start:

startService(new Intent(getApplicationContext(), MyService.class));

I am more confused than a mosquito in a mannequin shop. LogCat is returning absolutely nothing helpful other than u=0 not found.

Do I have something incorrect here? I'm not even seeing toasts from the service starting.

Upvotes: 0

Views: 58

Answers (4)

IAmTheSquidward
IAmTheSquidward

Reputation: 582

Ok, solved my own question.

MyService was running the whole time! I just didn't see the toast notifications to alert me when it started. Now I have a monitor in my main Activity that posts a toast when the service is started/killed, rather than using MyService itself to post toasts.

Upvotes: 0

Mikael Ohlson
Mikael Ohlson

Reputation: 3264

Try specifying the full name of your service as the android:name attribute (e.g. android:name="com.example.MyService")

Upvotes: 0

Qadir Hussain
Qadir Hussain

Reputation: 8856

try to Override the onStartCommand() method of your service. hope this helps

Upvotes: 1

johng
johng

Reputation: 1026

Make sure your service is declared in Android manifest

Upvotes: 1

Related Questions