how to stop the application from opening in android
I am developing a simple SMS Application in android,i am able to send and receive messages. during receiving of sms i want my application should open whenever the sms comes from a specified number with some notification within the app and it shouldnot open whenever it comes from unspecified number.. what i am able to do is open the app whenever the message comes fro开发者_如何学编程m the specified number but not able to stop my application from getting invoked(incase of unspecified number).
Help..
Your problem lies here:
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
phNum = msgs[i].getOriginatingAddress();
if("9716009159".equals(phNum)){
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
abortBroadcast();
}
else{
clearAbortBroadcast();
}
}
//---display the new SMS message---
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---launch the MainActivity--
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra("ph", phNum);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//---send a broadcast to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
The launching of your activity should only happen when the message is from the number you specify. At the moment you launch the activity as long as the message is not null. Instead of the code above, use this:
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
phNum = msgs[i].getOriginatingAddress();
if("9716009159".equals(phNum)){
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
//---display the new SMS message---
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---launch the MainActivity--
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.putExtra("ph", phNum);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//---send a broadcast to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
abortBroadcast();
}
else{
clearAbortBroadcast();
}
}
}
}
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();// note this line gives your contact number
str += " ::::::::::::::::::::";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
This code what you are writing in your broad cast reciver see // code which gives the incoming message number get the number from there and check the number with the number which you want to restrict if true then dont start you application if false start your application thats it
If you need more assistance comment me
It's not a good idea to open your application when the SMS comes. Imagine your user playing a game or watching youtube, and when the SMS comes your application appears in the foreground without user expecting it. This is not pleasant for your users. Good practice is to implement a service, that will scan received SMS and check whether number is specified or not. Then if number is specified you can add a notification to the notification bar to make user see the alert. When user clicks on your notification he opens your app and sees the information you provide. Hope this helps you.
You need to handle the sms received event in a broadcastreceiver, not an activity. In the broadcastreceiver's onReceive method you can filter the phone numbers, and if it is a phone number you're interested in, you can launch your activity.
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
if(phone number matches your filter){
startActivity(your intent);
}
}
}
}
And to declare the receiver in the manifest:
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
EDIT: The phone number you get from the system probably contains country code, area code etc, and therefore you should compare the strings like this:
if(extractedNumber.contains(yourNumber)){
//do your stuff
}
精彩评论