Wprowadzam następujący kod, w którym chcę uruchomić usługę przy użyciu odbiornika transmisji. Toast w odbiorniku transmisji działa dobrze, ale usługa nie jest wykonywana. Czy ktoś może mi powiedzieć, gdzie popełniłem błąd?Usługa uruchamiania systemu Android przy użyciu odbiornika transmisji
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//Toast.makeText(arg0, "Service", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(arg0,MyS.class);
arg0.startService(myIntent);
}
}
MyS.class
public class MyS extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Service started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.p"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:enabled="true"
android:name=".MyS" >
<intent-filter>
<action android:name="com.test.p.MyS" >
</action>
</intent-filter>
</service>
<receiver android:enabled="true"
android:name=".MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Należy zarejestrować transmisję w celu otrzymywania aktualizacji i wyrejestrować ją, gdy nie będzie ona korzystać z czynności. \t 'super.registerReceiver (mLoggedOutReceiver, new IntentFilter (LOG_OUT_ACTION));' – nesimtunc
Próbowałem, że oba sposoby ... rejestrowanie w manifeście Android iw klasie Nadal usługa nie jest uruchamiana. Czy możesz wysłać przykładowy kod lub link o tym, jak to zrobić? – Spike
Czy odpowiedź tutaj działa dla Ciebie? http://stackoverflow.com/questions/4641712/starting-service-from-broadcastreceiver –