How to copy some specific SMS content into clipboard? [closed]

瘦欲@ 提交于 2019-12-22 17:59:40

问题


The bank that I'm working with sends me an SMS containing a random generated six digit code whenever I want to enter for checking my accounts. And everytime it is really tiring to type it manually. I'm thinking about making a little application in order to copy that six digit number into the clipboard so that I paste it instantly to the related field whenever an SMS arrives. Could you please share your ideas on this subject with me?

Thanks, YB


回答1:


public class SmsListener extends BroadcastReceiver{

    private SharedPreferences preferences;

    @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();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                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();
                        String msgBody = msgs[i].getMessageBody();

                        // VALIDATE msgBody WITH SOME CONTENT YOU NEED...
                        // if(VALIDATED)
                        //     COPY msgBody TO CLIPBOARD
                    }
                }catch(Exception e){
                        // Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

Note: In your manifest file add the BroadcastReceiver-

<receiver android:name=".listener.SmsListener">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

Add this permission:

<uses-permission android:name="android.permission.RECEIVE_SMS" />

TO COPY TEXT TO CLIPBOARD USE BELOW LINK

Copy text to clipboard



来源:https://stackoverflow.com/questions/21722005/how-to-copy-some-specific-sms-content-into-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!