I want to start system application activity in broadcast onReceive() methord - android

I want to start a system application activity in a broadcast onReceive() method, but it cannot be run. I need help!
My Manifest.xml
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
My java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent b_intent = new Intent();
b_intent.setComponent(new ComponentName("com.android.email", "com.android.email.activity.Welcome"));
b_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(b_intent);
}
}
But this email application can not be run. There is only black color on the screen.
Thanks!

#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent()
i.setClassName("com.android.email", "com.android.email.activity.Welcome");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i); }

Launching system or third party apps from your app should be done through implicit intents. Hard-coding the package names and component names is not reliable and may not work all the time.
Morever, this particular activity, I guess, is not allowed to be called from other apps (my assumption, I might be wrong)

Related

How to pass data from one app to another app without opening the latter?

I want to trigger an application, B from application, A.
To achieve this i wrote the following in A
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.somepackage.appb");
intent.putExtra("secret", "message");
startActivity(intent);
However this opens up the application B which is not desired.
Please suggest a work around to avoid B from opening up and receive the data in background from application A.
Write a BroadcastReceiver in application B and declare it in your manifest.
In application A, craft an intent with extras to target that receiver, and call sendBroadcast with that intent.
Application B:
Manifest
<application>
...
<receiver android:name=".IncomingReceiver" android:enabled="true">
<intent-filter>
<action android:name="jason.wei.custom.intent.action.TEST"></action>
</intent-filter>
</receiver>
</application>
IncomingReceiver.java
public class IncomingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
if (intent.getAction().equals(CUSTOM_INTENT)) {
System.out.println("GOT THE INTENT");
Toast.makeText(context, "GOT THE INTENT", Toast.LENGTH_LONG).show();
}
}
}
Application A:
String CUSTOM_INTENT = "jason.wei.custom.intent.action.TEST";
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
sendBroadcast(i);
You can use SharedPreferences.
As you can see there are many ways to do this...myself i prefer creating a service which can update data without interacting with app B

Broadcast not triggering

I have been teaching myself android programming and I've run up against a problem with broadcasts. Essentially this code is just me testing to see if I can get broadcasts to work. When I run it my broadcast is not called and I'm not sure why.
Here is the relevant bit of manifest
<reciever
android:name="application.logic.StartEventReciever"
android:label="#string/title_activity_start_event" >
<intent-filter>
<action android:name="Set Start Alarm" />
</intent-filter>
</reciever>
Here is my BroadcastReciever
public class StartEventReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("#StartEvent", "BOO");
}
}
And here is the bit where I make the intent. It's a part of a larger static class I've been using.
public static void setStartAlarm(Context context){
Intent intent = new Intent("Set Start Alarm");
context.sendBroadcast(intent);
}
Your <reciever tag is spelled wrong. It is <receiver> </receiver>

Have BroadcastReceiver run as Service in the background + Auto Start after boot

I'm very new to Android and Programming in general, so I'm playing around with different
tutorials and info gathered here on stackoverflow.
What I would like to accomplish, is having the app with my SMS BroadcastReceiver run as a service, so I can get all the SMS broadcasts when app is in the background.
Also, how can I add a BroadcastReceiver for receiving broadcast of BOOT_COMPLETED and start app automatically?
Would I need several services for this, or is 1 service sufficient? (for detecting SMS + BOOT_COMPLETED continuously)
Currently I have a created a BroadcastReceiver for getting SMS, like this;
public class SMS extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
.. etc ..
.. etc ..
}
and my AndroidManifest.xml file has receiver and intent-filter with the
additional android.provider.Telephony.SMS_RECEIVED
Getting the SMS broadcast works fine, but I'm not sure where to go from here.
All help is much appreciated :)
Thanks.
To start your service on BOOT_COMPLETED event and to receive SMS intent continuously.
AndroidManifest.xml:
<receiver android:name="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, SMSService.class);
context.startService(service);
}
}
SMSService.java:
public class SMSService extends IntentService {
#Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (Intent.BOOT_COMPLETED.equals(action)) {
//write your code to process BOOT_COMPLETED intent here
}
else if(Intent.SMS_RECEIVED.equals(action)) {
//Write your code for processing SMS intent here
}
}
}
As, Fildor has pointed out, it is unnecessary to start service on BOOT_COMPLETED intent. InentService would do the work. So, above two code snippets are not required. Just the last snippet would do the work.

How can i get recevier instance(which is registered in AndroidManifest.xml) in activity

I've register a receiver in AndroidManifest.xml like this
<receiver android:name="com.sunrise.taximate.message.MessageRecevier">
<intent-filter>
<action android:name="xxx.xxxx.xxx.xxx" />
</intent-filter>
</receiver>
and now I want to get the receiver's instance in one of my activities(Like MainActivity),but I don't know how to. anyone can help me?
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Your code here to do what ever you want
}
}
Receivers are meant to act on events generated by the system and sometimes the users. There are special cases where you might want to get an instance of those yourself but this is uncommon. The whole point of having receivers is to react to system events and take some action. Unless you know what you are doing, I would recommend against creating receiver instances yourself in an activity.
If you really want to, you can do it like this
private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// do stuff
}
}
Also see this thread for related info:
BroadcastReceiver as inner class

How to Send Text Message to Android and Start Application or Application Activity

My question is very simple and I know the function can be preformed. I want to start activity via text message like "WHERE IS MY DROID" how can this be done? Please provide any information. I think SMSReceiver BroadcastReceiver is in the correct direction but I am not sure.
You need to register an Broadcast Receiver in AndroidManifest.xml for Receiving SMS_RECIVERD Broadcast as:
<receiver android:name=".SMSReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Add permission in AndroidManifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
in your Broadcast Receiver code start your Application as:
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//context.startService(new Intent(context, YourService.class));
//Start activity as:
Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
"com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
context.startActivity(intent24);
}
}
}
NOTE : For starting your Activity from background you need to set Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_FROM_BACKGROUND flags in intent for starting activity from background.
Check out this guy's sample code for intercepting SMS's http://imran-android-sms.blogspot.com/2011/03/receive-sms-on-android.html#more
From there you just want to fire off an intent to whatever your new activity should be.

Categories

Resources