用于启动应用程序的代码,用于从android中的特定号码接收SMS

时间:2011-04-26 05:21:40

标签: android

在那里,我想启动一个从特定号码或特定端口接收短信的应用程序。我正在尝试使用onReceiveintent,但我正在努力。那么,有谁在那里帮我详细?请用代码解释我。  谢谢

1 个答案:

答案 0 :(得分:1)

尝试这个...当你从5556端口收到短信时它会启动摄像头

 public class MySmsReceiver extends BroadcastReceiver {
        private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
        private String yourNumber = "5556";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Log.v("MySMS", intent.getAction());
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                final SmsMessage[] messages = new SmsMessage[pdus.length];
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                }
                if (messages.length > -1) {
                    String no = messages[0].getDisplayOriginatingAddress();
                    Log.v("MySMS", no);
                    if (no != null && no.trim().equals(yourNumber)) {
                        PackageManager manager = context.getPackageManager();
                        Intent resultIntent = new Intent();
                        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        resultIntent.setAction(Intent.ACTION_MAIN);
                        resultIntent.setComponent(new ComponentName(
                                "com.android.camera",
                                "com.android.camera.Camera"));
                        ResolveInfo ri = manager.resolveActivity(resultIntent,
                                Intent.FLAG_ACTIVITY_NEW_TASK);
                        if (ri != null) {
                            context.startActivity(resultIntent);
                        }
                    }
                }
            }
        }
    }
}

并且不要忘记在清单文件中添加此权限

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