I broadcast an intent like this
Intent intent= new Intent();
intent.setAction("com.my_app_two");
sendBroadcast(intent);
I only want com.my_app_two to get it.
I tried adding an extra to check but there has to be an easier way.
have you looked into using a Pendingintent instead of a broadcast as suggested in this answer as well ?
send broadcast intent to only one application without using explicit intent
The solution was to use setPackage
There I can specify which package should receive the intent.
Related
I am writing a code that shows some logs when user speaks through mic. For that purpose I'm using AudioManager.ACTION_AUDIO_BECOMING_NOISY intent but it does not work for me. I s there any broadcast intent for that purpose?
I have a intent service that receives explicit intents from other activities on what to do, and currently returns an implicit intent with the data as extras, like below:
Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
sendBroadcast(someImplicitIntent);
Software design wise, I have multiple activities within the app, that when running, would use the identical data for doing different things. So, currently I have setup broadcast receivers in each activity to listen for "APP_PACKAGE_NAME + EXTRA_SOME_DATA" and then act accordingly, like interrupting something. The broadcast receivers are tied to the their respective activities' life-cycles
This setup is fine for testing functionality. But the problem is, I understand that implicit intents in Android broadcasts the intents system wide, so any app which a registered broadcast receiver listening to the action would received the intent and its extra. Implicit intents are great for its intended purposes, but for me the convenience of minimal code and sending just one and multiple recipients are carrying obvious security risks.
I'm thinking of making explicit intents, but my current & limited understanding is I would have to create an explicit intent for each activity in order to keep the data private within the scope of the application, like below:
Intent someExplicitIntent= new Intent(this, activityOne.class);
someExplicitIntent.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent);
Intent someExplicitIntent2= new Intent(this, activityTwo.class);
someExplicitIntent2.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent2.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent2); ..... so on, for the same data to N activities
The snippet above look needlessly redundant, and will be wasting resources sending intents that won't be picked up. How can I securely send one intent extra to multiple receivers within an application?
You can make use of LocalBroadcastManager to send broadcasts which can be recieved only by the components of your application, thereby making it secure.
So, you can do as follows :
Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
LocalBroadcastManager.getInstance(this).sendBroadcast(someImplicitIntent);
Thereafter, just register your broadcast receiver in those components in which you want the intent extra/s.
In this way, you will be able to send intent extra/s through local broadcasts.
For more information about LocalBroadcastManager, you can check out the following link :
https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
Is there any event, method, flag or something else which tells me, that the next Activity has been started successfully and is shown on the screen.
MyCode to start an Activity class:
Intent intent = new Intent(this, nextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Thanks in advance
onCreate() is called when the activity is created, I would strongly recommend reading up on the Activity Lifecycle if you're not sure about this.
If you can modify the started activity why not just send an broadcast once new activity's onStart() called?
or do something else you need upon activity start
I need that my application restarts when click a button, for more info I need simulate thant I push back key and open application again.
I supose that i can close application with finish(); but how can i do to launch onCrete() again?
is there other way to do this?
thanks
Call your Activity using an Intent. It will launch the activity again.
Intent intent = new Intent(MyActivity.this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this will always start your activity as a new task
startActivity(intent);
You can also add, android:launchMode="singleTask" inside your Manifest so that only one instance of your activity will be maintained at any time.
Same thing here. :)
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
To restart your application you can use alarm manager to make your activity open after you exit the app.
AlarmManager alm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(), android.os.Process.SIGNAL_KILL);
I'm currently trying to place a call in the background.
Therefore I call this in my Main-Activity:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
I also wrote an Outgoing Call Receiver which changes the activity:
Intent myIntent = new Intent(context, DisplayCalcActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
The problem is now, that the call-screen is in the foreground. If I switch manually from the call screen to my app, the activity has changed as I wanted, but how is it possible to start the activity in the foreground, so that the call-screen won't be shown at all?
The Android SDK does not allow to make a call in "foreground" maybe you can do accessing to private members but this is not recommended. The only way to call is by dialer app.
Got the solution!
I used another Receiver to listen to "android.intent.action.PHONE_STATE".
The outgoing call, activates the receiver which starts the other activity.