I have two apps that I made, and am trying to send an intent from one to the other but the intent is never getting to the onReceive() however this problem is only one way. The first app can send to the second but the second cannot send back info. I am using a different intent action to send from the second to the first but otherwise they are identical. Any ideas on why this might not be working? I have tried everything I can think of and read most of the posts I could find on here and to no avail.
It's not crashing or giving me any indications as to what is happening in the logcat it's just doing nothing.
send function
private void sendFinishLog(String ID, String Cond)
{
Log.d("me", "send finish log");
Intent logIntent = new Intent();
logIntent.putExtra("ID", ID);
logIntent.putExtra("Cond", Cond);
logIntent.setAction("com.me.intent.finishlog");
Log.d("me","logIntent : " + logIntent.toString()
+logIntent.getExtras().toString());
sendBroadcast(logIntent);
}
receive class
public class LogReceiver extends BroadcastReceiver {
public static ArrayList<LogDataHolder> logData = new ArrayList<LogDataHolder>();
private boolean found;
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
private static String lasttime;
private static String now = "Boot time";
#Override
public void onReceive(Context cont, Intent logIntent)
{
Log.d("me","On receive");
etc.....
}
Receiving app manifest
<!-- for receiving logs -->
<receiver
android:name = "LogReceiver"
android:enabled="true">
<intent_filter>
<action android:name="com.me.intent.finishlog" />
</intent_filter>
</receiver>
something like:
public void onResume() {
IntentFilter filter = new IntentFilter();
getActivity().registerReceiver(mLogReceiver,filter);
}
and for un-register:
public void onPause() {
getActivity().unregsiterReceiver(mLogreceiver);
}
edit:
i just figured that your LogReceiver-class has no constructor neither. you will need to write one aswell:
private LogReceiver mLogReceiver = new LogReceiver() {
and after that you can use the onReceive-method like you have it already in your code.
Try it again like this. The category may need to be added
<receiver
android:name = "LogReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.me.intent.finishlog" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
My problem was solved when I removed the receiver unregister from the OnPause(). method.
You declared the receiver in the manifest as:
android:name = "LogReceiver"
The documentation states that name should be:
The name of the class that implements the broadcast receiver, a
subclass of BroadcastReceiver. This should be a fully qualified class
name (such as, "com.example.project.ReportReceiver"). However, as a
shorthand, if the first character of the name is a period (for
example, ". ReportReceiver"), it is appended to the package name
specified in the element.
In other words, it needs to include the package. Try changing the name to:
android:name = ".LogReceiver"
or
android:name = "com.me.yourpackagename.LogReceiver"
Related
This is what I am using right now:
in the onCreate method:
registerReceiver(bootup, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
the bootup receiver:
BroadcastReceiver bootup = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "received");
}
};
These are both in the MainActivity class of my app. What I want is for the bootup receiver to be called when the bootup of the phone is complete. I need it in the MainActivity class because I need to access a few things from it.
How would I set this? My current solution does not work.
EDIT: The posted solution seems as though it will work for my purpose, and this is what has been suggested by other threads. However, when I put a log statement in the receiver that it ties to, nothing appears in the console. Is this because the application is not running? I have also tried sending a notification with notificationmanager
EDIT 2: I took advice and switched to a broadcastreceiver in a another class for detecting the reboot. It works by simply changing the name attribute on the manifest file. This being the reason why I cant use an inner class receiver doesn't really make any sense compared to what I have seen on other questions. Can someone explain why I can't point the receiver to the inner class one and why I have to use a separate class?
Create a Receiver:
public class BootupReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
And receiver and uses-permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.restarttest.BootupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
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 am trying to listen to a very specific SMS sent from a particular sender containing a particular keyword. For that, I have created a BroadcastReciever which binds to android.provider.Telephony.SMS_RECEIVED through manifest.
If I find that particular sender sending SMS containing that keyword, I need to send the SMS from my application. I have done that in my application through onRecieve() function.
The problem is that I want to listen to SMS_SENT and SMS_DELIVERED events to know that if sms was successfully sent/delivered or not. For that , I am registering these receivers through
context.registerReceiver(smsSentReciever, new IntentFilter(Consts.SENT));
context.registerReceiver(smsDeliveredReciver, new IntentFilter(Consts.DELIVERED));
Though, I have instantiated a seperate AsyncTask from the onRecieve method to do this job, but still I am getting the error below
BroadcastReceiver components are not allowed to register to receive intents
Should I use IntentService instead of AsyncTask from the onRecieve?
or
Should I instantiate an IntentService from AsyncTask executed in onRecieve?
Just register your BroadcastReceiver in your manifest to handle all three Intents, and deal with them separately as they are received.
In AndroidManifest.xml:
<receiver android:name=".YourBroadcastReceiver">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<intent-filter>
<action android:name="com.mycompany.myapp.SMS_SENT" />
<action android:name="com.mycompany.myapp.SMS_DELIVERED" />
</intent-filter>
</receiver>
In YourBroadcastReceiver.java:
public class YourBroadcastReceiver extends BroadcastReceiver
{
public static final String ACTION_SMS_RECEIVED =
"android.provider.Telephony.SMS_RECEIVED";
public static final String ACTION_SMS_SENT = "com.mycompany.myapp.SMS_SENT";
public static final String ACTION_SMS_DELIVERED = "com.mycompany.myapp.SMS_DELIVERED";
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(ACTION_SMS_RECEIVED))
{
}
// etc...
}
}
Note: The SMS_RECEIVED priority is set to 999 so my app can handle the message before other apps, e.g., the platform SMS/MMS app.
My project is simple. It has an activity and a broadcast receiver. From within my app I want to be able to send sms and to receive sms. This works great.
But now I want to pass some data from the view to the receiver. Imagine a simple checkbox, I want to pass its value to the receiver.
So this is the basic life cycle of my app:
Start app
Press Send SMS
Receiver is started with params and sms is send
Receiver gets an sms and stops.
Receiver:
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Activity:
Receiver:
I tried to pass a value, but it seems to be ignored. Any ideas?
You cann't just start and stop a BroadcastReceiver at any time you want. BroadcastReceiver is only alive while onReceive() is executing.
To send a broadcast you should use sendBroadcast(this.service) instead of startService(this.service);
Once you change it to sendBroadcast(this.service) you will receive TWO broadcasts (first from your sendBroadcast() and second from SmsManager). This is definitely not what you want because in the first case you will be able to get your checked param but not SmsMessage and vice versa in the second case.
You can just store this checked param in the SharedPreferences and then retrieve it on onReceive()
Use Custom Intent Broadcasting to Achieve Current flow . make changes in your code as:
STEP 1 :
register an Custom Intent with SMS_RECEIVED in Manifest as :
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="xx.xxx.xxx.intent.action.SMS_STATUS_ACTION" />
</intent-filter>
</receiver>
STEP 2 :
public class SmsReceiver extends BroadcastReceiver {
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(SmsReceiver.STATUS_INTENT)) {
// get value here sended from Activity
}
else{
// Check for SMS_RECEIVED Action here
}
}
}
STEP 3 :
send value from your Activity as using sendBroadcast :
public static final String STATUS_INTENT =
"xx.xxx.xxx.intent.action.SMS_STATUS_ACTION";
#Override
public void onClick(View v) {
int checked = 0;
if(this.param.isChecked()){
checked = 1;
}
// put value here
Intent intent = new Intent();
intent.putInt("param", checked);
intent.setAction(CUSTOM_INTENT);
sendBroadcast(intent);
}
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>