Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a service automatically using broadcastreceivers after booting? [duplicate]

I am using android 4.4 version. How to start a service automatically using broadcastreceivers after booting? Thanks in advance. UPDATE: code in manifest file:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.prashanthi.trial.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>
    <service android:name=".WordService"></service>
     <receiver android:name=".MyScheduleReceiver" android:enabled="true"
              android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
</application>

Code in receiver class:

   public class MyScheduleReceiver extends BroadcastReceiver {

@Override
  public void onReceive(Context context, Intent intent) {

      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();        
      System.out.println("This is MyShedularReceiver");

   Intent service = new Intent(context, WordService.class);
      //service.setAction(RECEIVE_BOOT_COMPLETED);
    context.startService(service);
  }

   }

Code for service class:

 public class WordService extends Service{
public void onCreate() {

    super.onCreate();
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
        + (5 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set in 5 seconds",
        Toast.LENGTH_LONG).show();
    }


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
   }

}

In main activity I have code to display notifications. But the problem is, receiver class itself not triggered after running the app in emulator....can u please suggest me where I went wrong?

like image 836
user3057567 Avatar asked Dec 02 '22 20:12

user3057567


1 Answers

Follow this step one by one.

Create this Receiver class:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        /****** For Start Activity *****/
        Intent i = new Intent(context, MyActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);  

        /***** For start Service  ****/
        Intent myIntent = new Intent(context, ServiceClassName.class);
        context.startService(myIntent);
    }   

}

In AndroidManifest.xml:

<receiver 
    android:enabled="true" 
    android:name=".BootUpReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
<service
    android:name=".ServiceClassName"
    android:enabled="true"
    android:exported="true" >
</service>
like image 179
Satyaki Mukherjee Avatar answered Dec 05 '22 10:12

Satyaki Mukherjee