Not receiving a broadcast from my service to my Activity - android

I've looked at the numerous posts on sending messages from services to activities, but I just can't get this to work. My receiver is never being called. Does it matter if the Activity is not in the foreground?
Here is my scenario - I am running a web server as a service. When the web server receives a URL with link to a playlist, I need to send a message to my Activity that is acting as a media player. The media player will read the playlist and start playing the songs, videos, etc in order.
I don't want the service to start new instances of the media player activity, as sometimes it may be sending commands like fast forward or pause.
From my web server service:
private void sendMessage() {
Log.d("juice NonoHTTPD sender", "Sending message from Hub Web Server to Hub Player");
Intent intent = new Intent();
intent.putExtra(HubPlayer.ACTIVITY_PARAM_HOST, sAppHost);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_PORT, sAppPort);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_TYPE, sAppType);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_URL, sAppPath);
intent.setAction("com.jigawattlabs.hubplayer.play");
appContext.sendBroadcast(intent);
}
From my activity:
public class HubPlayer extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback
{
private IntentFilter intentFilter = new IntentFilter("com.jigawattlabs.hubplayer.play");
MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.mediaplayer_2);
registerReceiver(mReceiver , intentFilter);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
extras = intent.getExtras();
sHost = extras.getString(ACTIVITY_PARAM_HOST);
sPort = extras.getString(ACTIVITY_PARAM_PORT);
sType = extras.getString(ACTIVITY_PARAM_TYPE);
sXMLURL = extras.getString(ACTIVITY_PARAM_URL);
DebugMsg("received broadcast in MyBroadcastReceiver.");
processInputRequest();
}
}
}
Even though it doesn't seem necessary to put this in the manifest since I'm registering the BroadcastReceiver within my code, I also added an intent filter:
<activity android:label="Media Hub Player" android:launchMode="singleTask"
android:screenOrientation="unspecified"
android:name=".HubPlayer" >
<intent-filter>
<action android:name="com.jigawattlabs.hubplayer.play" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Thanks in advance.

OK, I have a workaround for this, but I was hoping for something better. Here is what I did
If the service needs to send a message to an activity, it checks to see if the activity is running
If the activity is running, it does the sendBroadcast as above
If the activity is not running, I do a StartActivity and pass it the same info as I would with sendBroadcast
From the activity, I pick up the intent either from the registered BroadcastReceiver or from the intent passed on onResume (if StartActivity is called).
Here is the code to check if an activity is running
public boolean isHubRunning()
{
boolean isServiceFound = false;
ActivityManager activityManager = (ActivityManager)appContext.getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
isServiceFound = false;
for (int i = 0; i < services.size(); i++)
{
//DebugMsg(services.get(i).topActivity.toString());
if (services.get(i).topActivity.toString().contains("juicemediahub.HubPlayer"))
{
isServiceFound = true;
}
}
DebugMsg("Hub Player Running = " + String.valueOf(isServiceFound));
return isServiceFound;
}

If you want to always get the intent, you should us a receiever. define it in your manifest and it will always capture your broadcast, activity running or not. something like:
<receiver android:name=".StartOnUser">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

Related

Callback from BroadcastReceiver beetwen Apps?

I want to allow other Apps to integrate with mine and I'm writing a dummy "consumer" app but I cant achieve to return a callback to notify the "consumer" app if everything went well.
So my DUMMY_APP has a simple layout with 2 buttons a success call, and a call with a wrong EXTRA param.
To make DUMMY_APP to call MAIN_APP I use sendBroadcast
// MainActivity class
private static final String REQUIRED_ACTION = "com.basetis.afr.intent.action.INIT_TEXT_FLOW";
onCreate....
Button btnSuccess = (Button)findViewById(R.id.button_success_call);
btnSuccess.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setAction(REQUIRED_ACTION);
i.putExtra(Intent.EXTRA_TEXT, textToBeRead);
sendBroadcast(i);
}
});
So MAIN_APP has the corresponding BroadcastReceiver that is receiving fine.
// BlinkingReadReceiver class
private static final String CALLBACK_CALL_AFR_ACTION = "com.basetis.afr.intent.action.CALLBACK_CALL_AFR_ACTION";
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "SUCCESS send callback");
i.setAction(CALLBACK_CALL_AFR_ACTION);
i.putExtra(CALL_AFR_SUCCESS_EXTRA, CALL_AFR_SUCCESS_EXTRA_DESC);
i.setType("text/plain");
context.sendBroadcast(i);
}
So the DUMMY_APP BroadcastReceiver never receive nothing :(
So I configured Manifests like that:
DUMMY_APP
<receiver android:name=".MainBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.basetis.afr.intent.action.CALLBACK_CALL_AFR_ACTION"></action>
</intent-filter>
</receiver>
MAIN_APP
<receiver android:name=".BlinkingReadReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.basetis.afr.intent.action.INIT_TEXT_FLOW"></action>
</intent-filter>
</receiver>
Sometimes I receive this error (afrsender is de DUMMY_APP) but seems sort of random...
Performing stop of activity that is not resumed: {com.basetis.afrsender.afrsender/com.basetis.afrsender.afrsender.MainActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed
Any suggestions about how to achieve this two way App communication?
Thank you very much.
As stated in the document
Starting from Android 3.1, the system's package manager keeps track of applications
that are in a stopped state and provides a means of controlling their launch from
background processes and other applications.
That means that till the app is not started manually by the user your app will be in force stop state and it won't receive any broadcast.
That's why your dummy app is not receiving and broadcast sent by main app.
Check here for more reference

I want to access and launch broadcast receiver from an activity and do somthing

I am trying to develop an android app, in which I want to sent a message from the phone while making call.
The destination number is taken from the application database.
I have completed till that part, but I cant access the broadcast receiver in my activity:
public class PARENT_CALLActivity extends Activity
{
/** Called when the activity is first created. */
String PARENT=null;
EditText edparent;
Button submit;
String parent_number;
public static final String BROADCAST = "sha.pcall.android.action.broadcast";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edparent=(EditText)findViewById(R.id.editText1);
submit=(Button)findViewById(R.id.btnsubmit);
submit.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
PARENT=edparent.getText().toString();
MyDabasehandler db=new MyDabasehandler(getApplicationContext());
if(db.getContact().equals(null))
{
db.addContact(new Contacts(PARENT));
}
else
{
db.editContact();
}
Intent intent = new Intent(getApplicationContext(),LocationUpdateReceiver.class);
sendBroadcast(intent);
finish();
}
});
}
public class LocationUpdateReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String outgoing_number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, outgoing_number, Toast.LENGTH_LONG).show();
}
}
}
If you are creating a separate class by extending Broadcast receiver I suggest you do it in a separate Class file. If you only want to receive broadcasts as long as the activity is open create a private Broadcast receiver variable. like in this question:
Where to register a BroadcastReceiver (Activity lifecycle fun)
In latter case you can register the broadcast receiver variable with this method called registerreceiver.
Here is the link
It all actually depends on the requirements. If you want to receive broadcasts even if your app is closed (or the activity is not in the foreground), you need to register your broadcast receiver in the manifest file like this:
<receiver
android:name="com.example.myapp.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.myapp" />
</intent-filter>
</receiver>
This is an example of google cloud message broadcast receiver. You also need to add intent filter, to specify which type of broadcast you want to receiver. In the above example the broadcast receiver can receive (see the intent-filter tag) two intents with actions:
"com.google.android.c2dm.intent.RECEIVE"
and
"com.google.android.c2dm.intent.REGISTRATION"
After you are done with this you can accomplish your tasks in the overridden onReceive() method of broadcast receiver.
Hope this helps.

Canceling broadcast receiver on phone reboot.

I have BroadcastReceiver (NetworkListener).
public class NetworkReceiver extends BroadcastReceiver {
Context context;
public static NetworkReceiver instance = new NetworkReceiver();
public static InnerObservable observable = instance. new InnerObservable();
...
This receiver sends notifications:
#Override
public void onReceive(Context ctx, Intent intent) {
Log.d("tag", "onReceive");
NotificationManager notif = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.ic_dialog_alert,"bla-bla-bla",System.currentTimeMillis());
Manifest file:
<receiver
android:name="com.mypckg.network.NetworkReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
It works fine. But after phone reboot, it continues to work. how to avoid it?
If you want your broadcast receiver to start working when application gets launched then you should register/unregister it programatically from your main activity:
private BroadcastReceiver networkReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
networkReceiver = new NetworkReceiver();
registerReceiver(networkReceiver, ConnectivityManager.CONNECTIVITY_ACTION);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkReceiver)
}
You can mess around with the PackageManager to disable a BroadcastReceiver registered in your manifest, check this thread for code.
Another solution would be to register this receiver dynamically, possibly in a service. The receiver would be active and registered as long as the service is alive, so you could easily toggle the receiver by starting/stopping the service. This might not fit you use case however, you didn't provide much detail on that.

In Android how do you register to receive headset plug broadcasts?

I am working in Android 2.1, and I want to detect when the headset is plugged in/taken out. I'm pretty new to android.
I think the way to do it is using a Broadcast receiver. I sublcassed this, and I also put the following in my AndroidManifest.xml. But do you have to register the receiver somehwere else, like in the activity? I'm aware there are lots of threads on this, but I don't really understand what they're talking about. Also, what's the difference between registering in AndroidManifest.xml versus registering dynamically in your activity?
<receiver android:enabled="true" android:name="AudioJackReceiver" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" >
</action>
</intent-filter>
</receiver>
And this was the implementation of the class (plus imports)
public class AudioJackReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.w("DEBUG", "headset state received");
}
}
I was just trying to see if it works, but nothing shows up when I unplug/plug in the headset while running the application.
EDIT: the documentation doesn't say this, but is it possible that this one won't work if registered in the manifest? I was able to get it to respond when I registered the receiver in one of my applications (or do you have to do that anyway?)
Just complementing Greg`s answer, here is the code that you need divided in two parts
Register the Service in the first Activity (here its called MainActivity.java).
Switch over the result of the ACTION_HEADSET_PLUG action in the BroadCastReceiver.
Here it goes:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MusicIntentReceiver();
}
#Override public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
super.onResume();
}
private class MusicIntentReceiver extends BroadcastReceiver {
#Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset is unplugged");
break;
case 1:
Log.d(TAG, "Headset is plugged");
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
}
Here are two sites that may help explain it in more detail:
http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
You have to define your intent; otherwise it won't access the system function. The broadcast receiver; will alert your application of changes that you'd like to listen for.
Every receiver needs to be subclassed; it must include a onReceive(). To implement the onReceive() you'll need to create a method that will include two items: Context & Intent.
More then likely a service would be ideal; but you'll create a service and define your context through it. In the context; you'll define your intent.
An example:
context.startService
(new Intent(context, YourService.class));
Very basic example. However; your particular goal is to utilize a system-wide broadcast. You want your application to be notified of Intent.ACTION_HEADSET_PLUG.
How to subscribe through manifest:
<receiver
android:name="AudioJackReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
Or you can simply define through your application; but. Your particular request; will require user permissions if you intend to detect Bluetooth MODIFY_AUDIO_SETTINGS.
You need to enable the broadcast receiver and set the exported attribute to true:
<receiver
android:name="AudioJackReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>

My Broadcast receiver get execute even if my application is not working

My broadcast receiver is Still getting execute even if My application is not working.
as an example I am using android.intent.action.NEW_OUTGOING_CALL to check outgoing call and than i stop music and push notification ..
but even i close my app and kill all task and after if i call than i get notification of my app..
So how do i manage to work my broadcast when i am using my app.
I have crated Service to play music and 2 broadcast receiver file for incoming and outgoing.
Help to solve this problem.
Also How can i destroy my app with service running behind If user press exit button.
**Update I made edited it and its working now fine..thank you so much you all
here is my code
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="OutgoingCallInterceptor">
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name="IncomingCallInterceptor">
<intent-filter android:priority="1">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
Update
as you all suggest me i have made receiver into main class file and register it from there but it wont work
public class MainActivity extends Activity {
RemoteViews layout;
int SDK_INT;
BroadcastReceiver br;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter("android.media.AUDIO_BECOMING_NOISY");
this.registerReceiver(br, filter);
setContentView(R.layout.activity_main);
SDK_INT = android.os.Build.VERSION.SDK_INT;
System.out.println(SDK_INT);
Button start = (Button)findViewById(R.id.play);
Button stop = (Button)findViewById(R.id.stop);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
context.stopService(new Intent(context, myPlayService.class));
Toast.makeText(context, "Headphones disconnected.", Toast.LENGTH_SHORT).show();
}
} }
};
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(br);
}
an example: how a broadcast reciver can be registered and un registered change as per your need "i hope the code is self explanatory"
private final BroadcastReceiver xyz= new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//ur reciver
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);//ur action
this.registerReceiver(xyz, filter);
}
#Override
protected void onDestroy(){
super.onDestroy();
unregisterReceiver(xyz);
}
You need to declare unregisterReceiver() in onResume() or in onpause() methods.
This will solve your problem.
First:
use unregisterReceiver() in onPause() and re-register in onStart().
It's always better to have locally registering the receiver if you want to provide the functionality only when your app is up.
Second:
Use service after binding it and don't call unBind while exiting the app will keep your service alive even after your app is down. I guess you are starting the service locally. Start service by binding it.
Hope this will solve your problem.

Categories

Resources