Android获取手机号码,后台发送短信以及监听短信发送结果。

时间:2021-09-13 08:57:09
TelephonyManager tm = (TelephonyManager) this.getSystemService(this.TELEPHONY_SERVICE);
phoneNumber1 = tm.getLine1Number();
Log.w("TelephonyManager", "phoneNumber1============" + phoneNumber1);

IntentFilter mFilter01;
mFilter01 = new IntentFilter(SENT_SMS_ACTION);
registerReceiver(sendMessage, mFilter01);

mFilter01 = new IntentFilter(DELIVERED_SMS_ACTION);
registerReceiver(receiver, mFilter01);

String phoneNumber1;
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";

private BroadcastReceiver sendMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("============", "send。。" + intent.getAction() + "====" + getResultCode() + "====" + getResultData());
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Log.w("============", "DXFSCG============" );
                break;
            default:
                Log.w("============", "DXFASB=========" );
                break;
        }
    }
};
private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("============", "DXFASB对方接收成功==========");
    }
};

private void sendSMS() {
    SmsManager sms = SmsManager.getDefault();
    Intent sentIntent = new Intent(SENT_SMS_ACTION);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
    Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
    PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0, deliverIntent, 0);
    sms.sendTextMessage("", null,  "xiao fa ", sentPI, deliverPI);
    Log.w("============", "============");
}