How to instantiate activity based on received sms? - android

How would I have an activity or animation start when the users device receives an sms? I want to start/play an animation when a sms comes in while they are viewing my app. How would I go about doing that?

public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
// here start the activity for animation or whatever you want to do
}
}
}
add this permission to your manifest
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
add the receiver under your application tag in your manifest
<receiver android:name=".SmsReceiver" >
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" >
</action>
</intent-filter>
</receiver>

Create a BroadcastReceiver for SMS
http://developer.android.com/reference/android/content/BroadcastReceiver.html
and act accordingly
SO Example if you had used search:
Android - SMS Broadcast receiver

Related

SMS_RECEIVED BroadcastReceiver does not fire when App process is inactive

I have registered a Broadcast receiver to listen to incoming SMS messages. I have observed that the Broadcast receiver does not fire, if my App process is not active.
AndroidManifest.xml
<receiver android:name=".SmsBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
SmsBroadcastReceiver.java
public class SmsBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = SmsBroadcastReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "New SMS-message received");
}
}
}
I have put Log messages on App's onCreate callback and I confirmed that App's onCreate is also not being called when SMS'es are received on the device.
Android Version - 6.0.1
Device - OnePlus 2

Launch single BroadcastReceiver form another application

I created an Application that shares a BroadcastReceiver that launches a Service.
The app that holds the receiver doesn't have activities, there are the receiver class and the services.
Here are the manifest declaration:
<service
android:name=".services.PrintService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".receivers.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="art.bridge.PRINT_MESSAGE" />
</intent-filter>
</receiver>
Receiver class
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d(ArtAppl.name , "Receiver reached");
Intent print = new Intent(context, PrintService.class);
if(intent.getStringExtra("message") != null)
print.putExtra("message",intent.getStringExtra("message"));
else
print.putExtra("message","Message not found");
context.startService(print);
}
}
Form an another app, i try to launch the receiver like following:
Intent bReceiver = new Intent("art.bridge.PRINT_MESSAGE");
sendBroadcast(bReceiver);
But the receiver doesn't run.
Am I missing something? Do I have to set another action in the manifest?
Could it be that you need to include Intent.FLAG_INCLUDE_STOPPED_PACKAGES as a flag?
See: How to Send BroadCast from one app to another app

How to cancel outgoing call in android dev.?

I just want to know if how to cancel outgoing call in android dev.? Is it possible in the simplest way of coding?
You need to create a BroadCastReceiver that will be called on the action : NEW_OUTGOING_CALL
Java Class :
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
setResultData(null);
abortBroadcast(); // cancel the call
}
}
}
Manifest declaration
Edit
don't forget add out going permission
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<receiver android:name=".CallReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
With this code, any call will be canceled and you will not be able to do a call if this app is on your phone
1.Create a BroadcastReceiver with a priority of 0.
<receiver android:name=".YourReceiver" >
<intent-filter android:priority="0" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
2.In the BroadcastReceiver intercept the ACTION_NEW_OUTGOING_CALL intent in its onReceive method
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
setResultData(null);
}
}
3.call setResultData(null) in the onReceive as shown in above code/method
Add below permission in AndroidManifest.xml file
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

Android BroadcastReceiver in separate project

I'm developing two Android applications.
The first one calls broadcast receiver from activity.
The second one contains broadcast receiver that is called from first application.
I've manage to do broadcast call when it is in the same application with caller activity.
But when I take receiver to separate project it doesn't work. What should I change?
This is how I register receiver:
<receiver
android:name=".TestReceiver"
android:enabled="true"
android:exported="true"
android:process=":deltaFO">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.myapp.intent.action.FILE_OPERATION" />
</intent-filter>
</receiver>
This is how I send intent
Intent intent = new Intent("com.myapp.intent.action.FILE_OPERATION");
intent.putExtra("operation", operation);
context.sendBroadcast(intent);
This is class that receives intent:
public class TestReceiver extends BroadcastReceiver{
public final String TAG="TestReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"EXTERNAL BROADCAST...");
}
}

How to show incoming call notification in android application

I want to display one dialog box after incoming call, so that I can run my application in background while receiving call.
How to catch that incoming call in android application???
In AndroidManifest.xml you shoud make a receiver:
<receiver android:name="IncomingCallInterceptor">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
and declare permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Then,
public class IncomingCallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
// Phone is ringing
}
}
}
Maybe this broadcast intent is what you need ACTION_PHONE_STATE_CHANGED

Categories

Resources