如何从Android的开放停止应用程序 [英] how to stop the application from opening in android

查看:104
本文介绍了如何从Android的开放停止应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发在Android中一个简单的短信应用程序,我能够发送和接收消息。
收到短信的过程中,我想只要短信来自于与应用程序内的一些通知中指定数量的我的应用程序应该打开,每当它来自数目不详它shouldnot开..
我所能够做的就是打开应用程序时的消息来自指定的号码,但无法从得到调用(柜面数目不详的),停止我的申请。

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 from the specified number but not able to stop my application from getting invoked(incase of unspecified number).

帮助..

推荐答案

您问题就在这里:

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);
        }                         
        }

在邮件是由您指定的号码你的活动的开展应该只发生。此刻的你,只要消息不是空发射活动。相反,$ C $上述C,使用:

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();
                }
            }
        }                         
    }

这篇关于如何从Android的开放停止应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆