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>
Related
I have used BootComplete and allow permission and it still cant autostart,then I try to use wake lock but it cannot work. Also, I try to make it as a service but the service does not pop up in my phone.Is there anything I missed?
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
{
// This is the Intent to deliver to our service.
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
public class AutoStartUp 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
}
}
In my manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".SimpleWakefulReceiver">
<intent-filter>
<action android:name="com.example.SimpleWakefulReceiver"/>
</intent-filter>
</service>
<receiver
android:name=".MainActivity$BootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".AutoStartUp">
</service>
Do whatever you intend in onReceive -
public class BootupReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("BOOTUP", "received notification ......................");
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
Log.e("BOOTUP","RECEIVED BOOT NOTIFICATION ........");
Intent start_service = new Intent(context,MainService.class);
context.startService(start_service);
}
}
in the Manifest add-
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
add permission-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also you need to launch your application from activity atleast once and your block should be outside block in manifest
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.
I defined two background services for my app. But only one of the, the AttachService starts. In LogCat I can see the starting log messages of the attachService but nothing from the PenManagerService. I defined for each service a startup receiver
public class StartupCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, PenManagerService.class);
context.startService(service);
}
}
public class AttachmentStartupReceiver extends BroadcastReceiver {
private static final String TAG = AttachmentStartupReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
Intent attachSvc = new Intent(context, AttachService.class);
context.startService(attachSvc);
}
}
Here the definition of the two services in the manifest
<service
android:name="de.android.services.PenManagerService"
android:process=":PenManagerService" >
</service>
<service
android:name="de.android.services.AttachService"
android:icon="#drawable/loading_icon"
android:label="#string/attachServiceName"
android:process=":attachServiceBackground" />
<receiver
android:name="de.android.services.StartupCompletedReceiver"
android:process=":PenManagerService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="de.android.AttachmentStartupReceiver"
android:process=":attachServiceBackground" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Both services extends the IntentService and in their onCreate I have a log message, but only the one from the attachService shows up in the log.
I found lots of threads about this topic, however I'm not able to solve my problem. Here is the code:
The manifest file:
<service
android:name="com.xxx.player.MediaPlayerService.MediaPlayerService"
android:enabled="true" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<receiver android:name="com.xxx.player.MediaPlayerService.MediaPlayerService$ServiceBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="#string/ACTION_PREPARE_AND_PLAY_NEW_FILE"/>
<action android:name="#string/ACTION_PREPARE_NEW_FILE"/>
<action android:name="#string/ACTION_START"/>
<action android:name="#string/ACTION_STOP"/>
<action android:name="#string/ACTION_PAUSE"/>
<action android:name="#string/ACTION_SEEK_TO"/>
<action android:name="#string/ACTION_RELEASE"/>
</intent-filter>
</receiver>
</service>
The broadcast class:
public class MediaPlayerService extends Service implements
MediaPlayer.OnErrorListener,
AudioManager.OnAudioFocusChangeListener,
Runnable,
SeekBar.OnSeekBarChangeListener {
...
public class ServiceBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "action: " + action, 30000000).show();
if (action.equals(Resources.getSystem().getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE))) {
prepareAndPlayNewFile(intent.getStringExtra("mediaData"));
}
}
}
}
The way I submit the intent:
private void prepareAndPlayNewFile(String mediaData) {
Intent myIntent = new Intent();
myIntent.setAction(context.getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE));
myIntent.putExtra("mediaData", mediaData);
context.sendBroadcast(myIntent);
}
Instead of approaching your playing of media this way, you should instead bind your Service to your Activity instead of using a broadcast receiver for message passing.
Also you shouldn't be using #string/VAR1 (which I'm not sure if intent filters work with that kind of string definition) for your intent actions it ALWAYS should be the constant string such as:
android.intent.action.BLAH
So I have a boot receiver that is supposed to call an intent service but the receiver isn't registering at all.
Manifest file -
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".ClockReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
ClockReceiver.java
public class ClockReceiver extends BroadcastReceiver {
private final String TAG = "ClockReciever";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"onRecive");
context.startService(new Intent(context, RefreshIntentService.class));
}
}
I think this is correct, but according to my logcat the ClockReciever is never called and the program crashes with a "Unable to instantiate receiver" error.
Any suggestions? Thank you
Here you have a typo
<receiver android:name=".ClockReciever">
Should be ClockReceiver, i.e. same as your class.
Cheers!