Reputation: 4543
In my Android application I want to make a call as a response to a special SMS. So I created app which listens for in coming messages and make a call because of a particular sms. The application works on emulator as expected but when I'm trying to run application on tab which has Android 4.0.3 it ends calling just after starting the call. Here is the code I have used.
public class MainActivity extends Activity {
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
String msgBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(broadcastReceiver, filter);
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
}
}catch(Exception e){
}
}
}
}
Toast.makeText(getApplicationContext(), msgBody, Toast.LENGTH_LONG).show();
if(msgBody.equals("CALL")){
Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "XXXXXXXXXXX"));;
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have added following permissions:
READ_SMS
RECEIVE_SMS
CALL_PHONE
What is the wrong here ? Please help me.
Thanks in advanced.
Upvotes: 1
Views: 1994
Reputation: 8034
Create another activity say CallActivity, start this activity from your receiver e.g
Intent i=new Intent();
i.setClass(context,callActivity.class);
i.putExtra("number","phone_number");
i.setFlags(Intent.Flag_Activity_newTask);
context.startActivity(i);
Now in onCreate() method of CallActivity, just launch call phone intent:
Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "XXXXXXXXXXX"));;
startActivity(myIntent);
Upvotes: 1