I am currently trying to make a broadcast receiver which will invoke after android device boots and then will run a background service. I have tried many examples but don't know where I'm going wrong. I am following this example: https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot I have imported this whole project in my workspace and tried to run. But the receiver didn't invoked or so. Please help me out. My Testing Device is: Motorolla Xoom with ICS 4.0.3
EDIT
Manifest
<uses-sdk android:minSdkVersion="8" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REBOOT" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name="awais.soft.MyService"
android:enabled="true" >
<intent-filter>
<action android:name="awais.soft.MyService" >
</action>
</intent-filter>
</service>
<receiver android:name="awais.soft.ServicesDemoActivity" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
</application>
Broadcast Receiver
package awais.soft;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class ServicesDemoActivity extends BroadcastReceiver {
static final int idBut = Menu.FIRST + 1, idIntentID = Menu.FIRST + 2;
@Override
public void onReceive(Context context, Intent intent) {
Log.e("Awais", "onReceive:");
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Intent i = new Intent();
i.setAction("awais.kpsoft.MyService");
context.startService(i);
}
}
}
Service
package awais.soft;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.is);
player.setLooping(false); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
see i am posting you eample that will help you
For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events). At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.
Step 1: First you'll need to create a simple service, defined in Monitor.java:
}
Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:
}
Step 3: Finally, your AndroidManifest.xml file must contain the following:
I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest
If your phone is rooted then you will have trouble in Android Boot-Up BroadCast invoking otherwise you have to ensure your app has required root permissions
The problem persists in the case of devices having android version more than 3.0, by the way its not the problem it has been done for security purposes by google i guess..If u have to run the service on boot you have to make a custom intent & broadcast it. For making custom intent you have to make a service file from where u have to broadcast that intent on boot complete & your service file(that u want to run) will receive that intent on its onReceive method & your service will run.One more thing the service file you will create to call your service that you want to run should be kept on system/app folder of file explorer of device, if your file system shows sorry read only file system then from command prompt do just adb remount & then push the file on device,restart your system your service will run..Cheers!!
Everything was fine..:S The problem was with device..(i.e. Motorolla Zoom ICS 4.0.3)
Now tested on Galaxy Tab With 2.2 and Working fine..
Thanks all for your time
I am something like this in My app and Its Working for me.
Android Manifset
Please check if you have given permission for RECEIVE_BOOT_COMPLETED