Differents between sendBroadcast (intent) and startActivity(intent) in android? - android

When i send a broadcast by context.sendBroadcast(intent1) method with parameter: intent1 and start a activity by context.startActivity(intent2) method with parameter: intent2. What is difference between them. Are intent1 and intent2 implicit intent with define: new intent(action_do_something). Can anyone help me ? Thank a lot

As the names suggest, sendBroadcast will send a message to no particular recipient. It just transmits a message like a radio tower. You have to listen for broadcasts.
On the other hand, startActivity starts an activity(the onResume() of the activity will be eventually called).

The names are saying it all. When you send a broadcast message you need to call the sendBroadcast. but to start activity u need the other method. There is no direct comparision between these two as the purpose of these two things is totally different.
In both case the intent is used for a common reason, first two define the receiver. In the broadcast message the intent is passed to make sure which type of receiver can catch this. And for startActivity it is used to do same but for to make sure which activity will be started. and in both cases intent is used to pass data.

Related

In Broadcast receiver class, onReceive method contains custom intent which is not working in Samsung mobile

Here i am trying to build a simple sms receiving app. In Broadcast receiver class,
I have the onReceive method which contains a custom intent which is not working in Samsung Mobile(4.1.2) but it works in kitkat 4.4(moto e).
Intent in = new Intent("SmsMessage.intent.MAIN").putExtra("get_body", sms_body);
context.sendBroadcast(in);
How do I do this in Android 4.1.2?
All you need to do if send some data via an intent right?
If no please do explain what you mean by broadcast not working.
If yes the put the following inside your onReceive() method.
Intent in = new Intent(context, ****Name of your activity****.class)
in.putExtra("get_body", sms_body)
context.startActivity(in);
Please do take note that where I have mentioned _****Name of your activity****_change it to the name of your activity
Inside your activity create a method onNewIntent ()
Inside which input the following code:
String textInSms = intent.getStringExtra("get_body")
That's it now you can do whatever you want with the textInSms string. It holds the value of whatever extra you passed to it.

How can I know the startActivity intent from other application?

I have an Activity and assume that has been launched. When the other application use startActivity method to start my Activity, my Activity will show and run the onResume Method, but I can't find any way to get the intent which is used in startActivity method by the other application. I want to get the extra data in the intent. How can I do?
EDIT
My Activity is singleTask, and I want to get the startActivity intents form other applications. I think it is not associate with filters.
Have you tried using getIntent() ?
Then you can do:
this.getIntent().getExtras();
After that if you need new intents just override the onNewIntent function in your activity.
I simply say an example. When we need to share something. We click share button which shows a list of app by which we can share our things.
So, if you want to make that kind of app which can receive other app data then you need make your activity capable of receiving that data. In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter in manifest.
Below this link you will get some more information : http://developer.android.com/training/basics/intents/filters.html

Do implicit intents do a broadcast internally?

Following piece of code open an URL using implicit intent.
EditText editText = (EditText) findViewById(R.id.url_editText);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(editText.getText().toString()));
startActivity(myIntent);
It shows up all the applications that support this action Intent.ACTION_VIEW like Chrome, Firefox etc. Hence I am assuming this procedure internally does a broadcast with action Intent.ACTION_VIEW. Please correct me if I have misunderstood.
As per above assumption I tried the foloowing code
Intent myIntent = new Intent(Intent.ACTION_VIEW);
sendBroadcast(myIntent);
but it does not work. Nothing is shown. What is the difference. Can someone clear my confusion?
This has to do with IntentFilters. Before launching the Activity, it's asking the system to give it the list of everything that's an Activity and can handle the intent -- no broadcasting involved here.
As for sendBroadcast() -- it's essentially the same thing but for BroadcastReceivers. The mechanism is the same: match intent filters, deliver the Intent, but the Intent is delivered to all the receivers regardless of their quantity (as opposed to what startActivity() does -- because it can only result in starting a single activity, hence the need to choose one if there are multiple that match).
I don't think there are any BroadcastReceivers registered for Intent.ACTION_VIEW (since it's an action whose purpose is to start an activity, there's no logical reason to listen for it and start nothing, except count activity launches or something) but you can register one yourself and see what happens.
Hence I am assuming this procedure internally does a broadcast with action Intent.ACTION_VIEW.
No.
but it does not work. Nothing is shown
Of course.
What is the difference.
startActivity() != sendBroadast(). They are separate operations, just as addition and subtraction are separate mathematical operations.
If you wish to think of the Intent system as being a bit like a message bus, that bus has three totally separate channels:
activities (startActivity())
services (startService(), bindService())
broadcasts (sendBroadcast(), sendOrderedBroadcast(), etc.)
The difference between those two is just who receives the Intent. If you call sendBroadcast() the Intent will be sent to BroadcastReceivers. If you call startActivity() the Intent will be sent to Activities. That's the reason why this:
Intent myIntent = new Intent(Intent.ACTION_VIEW);
sendBroadcast(myIntent);
Doesn't start an Activity, because the Intent is only visible to BroadcastReceivers.
The same goes for startService(). By calling that method the Intent will only target Services. I guess the confusion comes from the word broadcast. It implies that it is sent everywhere and visible to everyone, but that is not the case. Broadcasts are only visible to BroadcastReceivers just like if you call startActivity() the Intent will only target Activities and nothing else.
You can find more information here.

Setting parameters for activities / services

Is there a way to set parameters that they would receive as arguments in the constructor were they initialized as standard objects instead of by intents? I can't use parameters stored in the intent because sending an intent doesn't necessarily initialize the activity / service (it may exist already). Can I use the manifest file to set custom parameters?
Thanks.
Starting an Activity will always start activity, and if activity is already in stack, then also it would launch the activity, unless some intent filter havent been specified.
For Service, if you start service by startService(), onStartCommand() method will be invoked, which has an Intent as parameter, you can get values passed from this parameter.
sending an intent doesn't necessarily initialize the activity / service(it may exist already).
What if you keep using Intents but erase the history of the TargetActivity each time you call it.
Intent intent = new Intent(this,TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I found a possible solution here: Is it possible to have custom attributes in AndroidManifest.xml tags?
Doesn't help if I want to pass objects, but for simple types it's good enough.

Android: how to call a non-static function from C2DMBaseReceiver?

I am using android-c2dm, and my device successfully receives messages from it. I want to call a non-static function (in an already-existing Activity) to do something with that message, but simply calling it from C2DMBaseReceiver is illegal. How can I transfer this information back to the activity?
Edit: What if I call a static function to assign variables (or set Shared Preferences), then call a handler which will use those variables to do what needs to be done? Is that bad style?
What you could do is put the message details in an Intent somehow (the crudest way would be serialize the message to a String and add it as an Intent extra) and then send that Intent to the Activity using startActivity. The Activity could check for the extra, know that it is a message, extract and deserialize the message and then go to town.
You might need to set the appropriate launch mode or Intent flags if you want to make sure that an existing instance of the target Activity receives the message.
You can get message from Intent in onMessage method, then show Notification and startActivity after user clicks on Notification. You can use Intent flags for bring background activity in foreground.
So if activity is not started - it will be started, if activity in background - it will be show in foreground and if it in foreground - then we need only change TextView text.
You also can startActivity without showing Notification.
Also you can use onNewIntent(Intent intent) Activity method for changing text. You can put message from google intent in onMessage into new Intent and startActivity with this intent and FLAG_ACTIVITY_SINGLE_TOP flag.

Categories

Resources