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
Related
I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
So in fragment class, I have a AsyncTask class and there I am calling BroadcastReceiver at onPostExecute() method of AsyncTask -
Android Manifest -
<receiver
android:name="com.iddl.main.IncomingBBStream"
android:label="IncomingBBStream">
<action android:name="com.iddl.main.BBbroadcast" />
<intent-filter>
<action android:name="android.bluetooth.
device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
Fragment Class -
#Override
protected void onPostExecute(Void result)
{
Intent intent = new Intent();
intent.setAction("com.iddl.main.BBbroadcast");
intent.putExtra("uuid", uuid);
getActivity().sendBroadcast(intent);
}
BroadcastReceiver Class -
public class IncomingBBStream extends BroadcastReceiver {
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "onReceive BroadcastReceiver------");
}
It should at least print the log message but it not doing so.
NOTE:
I have to add intent.setClass(getActivity(), IncomingBBStream.class); to get it work but that doesn't make sense because I already passed the action "com.iddl.main.BBbroadcast" that matches with the manifest's receiver name "com.iddl.main.IncomingBBStream". So it knows which class to call.
you probably want to do that:
<receiver
android:name="com.iddl.main.IncomingBBStream"
android:label="IncomingBBStream">
<intent-filter>
<action android:name="com.iddl.main.BBbroadcast"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.bluetooth.
device.action.ACL_DISCONNECTED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
each intent filter separate on its own tag and always include the default category.
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 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>
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!