我正在使用的银行向我发送一条短信,其中包含随机生成的六位数代码,只要我想输入以检查我的帐户。每次手动输入它真的很累。我正在考虑制作一个小应用程序,以便将这个六位数字复制到剪贴板中,这样我就可以在短信到达时立即将它粘贴到相关字段。你能和我分享你对这个主题的想法吗?
谢谢, YB
答案 0 :(得分: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());
}
}
}
}
}
注意:在清单文件中添加BroadcastReceiver -
<receiver android:name=".listener.SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
添加此权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
将文字复制到剪贴板以下链接