接收短信并显示通知

时间:2017-01-10 18:18:51

标签: android android-studio smsmanager

我正在尝试构建一个显示SMS接收通知的代码。我的问题是如何在我的主要活动中实现广播接收器,因为在我的主要活动中我还有其他功能正常工作。如果我尝试创建另一个扩展广播接收器的类,那么如何从主类中调用它?它是在收到消息后立即自动启动该功能还是需要从主活动中激活?

4 个答案:

答案 0 :(得分:1)

你看到this的问题了吗? 无论如何试试这个:

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

您也可以使用Notification.Builder

制作通知

答案 1 :(得分:0)

如果我是你,我会在你传递捆绑值的广播接收器的onReceive方法中放置一个intent调用,然后在mainactivity上查看是否存在该bundle,如果是,你做什么动作你想做什么(例如,显示对话框,吐司等)。

答案 2 :(得分:0)

只要您正确注册(通过代码或清单xml),

广播接收器将由Android调用。您将获得访问者回调中context的访问权限。从该上下文中,您可以创建通知(基于此问题的标题)或访问applicationContext,这将是您在清单xml中声明的Application子类 IF

答案 3 :(得分:-1)

@another,尝试按以下方式实施:

public class MyActivity extends Activity {
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }   
   });

    public void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);

        this.registerReceiver(this.receiver, filter);
    }

    public void onPause() {
        super.onPause();

        this.unregisterReceiver(this.receiver);
    }
}