Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid BroadcastReceiver, auto run service after reboot of device
    primarykey
    data
    text
    <p>Hello i am writing an application, which is when the phone reboot, the service will auto start instead of click on the application.</p> <p>Here is my code for </p> <p><strong>BootCompleteReceiver.java</strong></p> <pre><code>package com.example.newbootservice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, MsgPushService.class); context.startService(service); } } </code></pre> <p><strong>MsgPushService.java</strong></p> <pre><code>package com.example.newbootservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class MsgPushService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); return Service.START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent arg0) { return null; } } </code></pre> <p><strong>MainActivity.java</strong> (not sure whether i need this)</p> <pre><code>package com.example.newbootservice; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startService(new Intent(getBaseContext(), MsgPushService.class)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p><strong>Manifest</strong></p> <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.newbootservice" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;service android:name=".MsgPushService"/&gt; &lt;receiver android:name=".BootCompleteReceiver"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;activity android:name=".MainActivity" android:label="@string/title_activity_main" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>I want the service to be started automatically after reboot instead of starting it manually.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload