Start Service from BroadcastReceiver never call service.onStartCommand() method - android

I have music service that playing audio files. I start this service from my activity. I send notification with play/pause button when i click on play button from notification i send broadcast receiver and it's working. In on onReceive(Context context, Intent intent) method i call context.startService(intentService) the problem is MusicService.onStartCommand() never called.
My receiver
public class PlayEpisodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service Started", Toast.LENGTH_SHORT).show();
if(MusicService.isRunning()) {
Intent serviceIntent = new Intent(context, MusicService.class);
serviceIntent.putExtra(AppConstants.ACTION_TYPE, AppConstants.ACTION_PLAY);
context.startService(intent);
}
}
}
My manifest:
<service
android:enabled="true"
android:name=".backgroundtasks.MusicService">
<intent-filter>
<action
android:name = "cc.gm.oscarradio.backgroundtasks.MusicService">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".Receivers.PlayEpisodeReceiver">
<intent-filter>
<action android:name = "cc.gm.oscarradio.ACTION_MUSIC"/>
</intent-filter>
</receiver>

In your code there should be a slight change I feel, you should replace the following line:
context.startService(intent);
by
context.startService(serviceIntent);
Hope this helps you.

Related

Android app doesn't start at system boot

I've a reciever
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myStarterIntent = new Intent(context, MainActivity.class);
context.startActivity(myStarterIntent);
}
}
and have modified the AndroidManifest.xml, adding these lines
<receiver
android:enabled="true"
android:name=".MyBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
to section.
The application still does not start on system boot..any ideas appreciated. At least how can I monitor what is going on after device reboot (because I cant just use breakpoints in that case)
You need to provide Intent.FLAG_ACTIVITY_NEW_TASK flag when starting activity from BroadcastReceiver.
Intent myStarterIntent = new Intent(context, MainActivity.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
This is how you can test the BroadcastReceiver.

Boot Complete Broadcast not Working

I am trying to start an activity when am restart my phone then its open app or show me toast when booting is complete
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyIntentService.class);
context.startService(serviceIntent);
}
}
}
this is my Broadcaste receiver Code
class MyIntentService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
This is my service Code.
Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyIntentService"></service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
am trying lots of diffrent code but no one work for me so can anyone help me to correct this code
Your BroadcastReceiver will never get called because you have this in the manifest entry for it:
android:exported="false"
Remove that.
NOTE: You also need to make sure that your app is started at least once manually after installing it on the phone. Otherwise your BroadcastReceiver will NOT get the BOOT_COMPLETE Intent.
NOTE: Also, using Toast as a debugging aid isn't a very good idea. You should write messages to the logcat and use that to determine if your Service is getting started, etc. Toast is not reliable as a debugging tool.
Add this in BroadcastReceiver class
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, SyncData.class);
context.startService(pushIntent);
Log.e("BroadCast Received", "ON BOOT COMPLETE");
}
}
and remove this two lines android:enabled="true"
android:exported="false"

Call Broadcast Receiver from another process

In my application i have to use a BroadcastReceiver which must run in it's own process.
<receiver
android:name="com.greenroad.mobile.asimov.tiles.AsimoveTileRequestUpdateReceiver"
android:process="com.zonarsystems.Sample2020App.tile" >
<intent-filter>
<action android:name="com.zonarsystems.twenty20.tile.intent.action.TILE_REQUEST_UPDATE" />
</intent-filter>
</receiver>
The application sending data to this receiver in order to process them.
for calling this receiver the application using
Intent intent = new Intent("com.zonarsystems.twenty20.tile.intent.action.TILE_REQUEST_UPDATE");
intent.putExtras(indicationsBundle);
sendBroadcast(intent);
But i get nohing in
#Override
public void onReceive(Context context, Intent intent) { ... }
How it can be solved?
Thanks,
Eyal.

cannot start my service when receive SMS

I have a COMPLETED ON BOOT service in which a music starts immediately should I receive an SMS.
Am I doing something wrong?
public class MyService extends Service {
final MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.son1);
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mPlayer.start();
}
}
};
}
<service android:name=".MyService"/>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
I suggest you to use android:priority="999" in your AndroidManifest.xml as follows,
<receiver
android:name="com.application.reciever.SMSReceiver"
class="com.application.reciever.SMSReceiver" android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This way your application get's priority on SMS Receive event.
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context mContext, Intent intent)
{
// Your code to play music
}
}
You probably miss the permission to receive SMS_RECEIVED broadcasts. Try adding to your manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

Android BroadcastReceiver in separate project

I'm developing two Android applications.
The first one calls broadcast receiver from activity.
The second one contains broadcast receiver that is called from first application.
I've manage to do broadcast call when it is in the same application with caller activity.
But when I take receiver to separate project it doesn't work. What should I change?
This is how I register receiver:
<receiver
android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:process=":deltaFO">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.myapp.intent.action.FILE_OPERATION" />
</intent-filter>
</receiver>
This is how I send intent
Intent intent = new Intent("com.myapp.intent.action.FILE_OPERATION");
intent.putExtra("operation", operation);
context.sendBroadcast(intent);
This is class that receives intent:
public class TestReceiver extends BroadcastReceiver{
public final String TAG="TestReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"EXTERNAL BROADCAST...");
}
}

Categories

Resources