自动短信读取不在Android中工作

时间:2017-02-15 06:40:58

标签: android broadcastreceiver android-broadcastreceiver smsmanager

public class SmsReceiver extends BroadcastReceiver {

    private static SmsListener mListener;

    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle data = intent.getExtras();

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

        for (int i = 0; i < pdus.length; i++) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);

            String sender = smsMessage.getDisplayOriginatingAddress();
            //You must check here if the sender is your provider and not another one with same text.

            Log.i("Sender id ",sender);

            String messageBody = smsMessage.getMessageBody();
            Log.i("get Message body ",messageBody);
            messageBody=smsMessage.getDisplayMessageBody();
            Log.i("Message Body ",messageBody);

            //Pass on the text to our listener.
            mListener.messageReceived(messageBody);
        }
    }

    public static void bindListener(SmsListener listener) {
        mListener = listener;
    }

}

public interface SmsListener {
    public void messageReceived(String messageText);
}

在fragement /对话框中呼叫:

SmsReceiver.bindListener(new SmsListener() {
            @Override
            public void messageReceived(String messageText) {
                Log.d("Text",messageText);
                Toast.makeText(getContext(),"Message: "+messageText,Toast.LENGTH_LONG).show();
            }
        });

 @Override
    public void onResume() {

        Log.i("In resume ","resume");
        SmsReceiver.bindListener(new SmsListener() {
            @Override
            public void messageReceived(String messageText) {
                Log.d("Text",messageText);
                Toast.makeText(getContext(),"Message: "+messageText,Toast.LENGTH_LONG).show();
            }
        });

        super.onResume();
    }

  @Override
    public void onPause() {
        Log.i("In pause ","pause");
        super.onPause();
         SmsReceiver.bindListener(new SmsListener() {
            @Override
            public void messageReceived(String messageText) {
                Log.d("Text",messageText);
                Toast.makeText(getContext(),"Message: "+messageText,Toast.LENGTH_LONG).show();
            }
        });

    }



<receiver android:name="services.SmsReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
     <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.SEND_SMS" />

我没有收到任何错误,也没有在日志中打印任何内容。不知道发生了什么错误。甚至没有来自广播接收器的打印。我认为onResume中的代码也没有执行。

2 个答案:

答案 0 :(得分:1)

对于小米权限对话框使用此阅读所有短信

   private void displaySmsLog() {
        Uri allMessages = Uri.parse("content://sms/");
        //Cursor cursor = managedQuery(allMessages, null, null, null, null);  Both are same
        Cursor cursor = getActivity().getContentResolver().query(allMessages, null,
                null, null, null);

   if (cursor!=null) {

    while (cursor.moveToNext()) {
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            Log.d(cursor.getColumnName(i) + "", cursor.getString(i) + "");
        }
        Log.d("One row finished",
                "**************************************************");
    }
}
        else {


}

    }

希望这会有所帮助。:)

答案 1 :(得分:0)

由于它是一个xiaomi设备,它拒绝短信读取/接收权限要么你必须手动打开权限,或者你可以尝试下面的代码让xiaomi知道你的应用程序需要读取/接收短信它会显示一个权限对话框读取短信: -

 Cursor cursor = getActivity().getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    if (cursor == null) {
        return;
    }
    try {
        if (cursor.moveToFirst()) {
            // must check the result to prevent exception
               //This will show a permission dialog to let app read /receive sms
                }
            } while (cursor.moveToNext());
        } else {
            Log.e(this.getClass().getSimpleName(), "No SMS");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }