s39014971 2016-03-13 08:32 采纳率: 50%
浏览 1396
已采纳

Android 接收短信功能

package com.example.smstest;

import javax.security.auth.PrivateCredentialPermission;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView sender;
private TextView content;
private IntentFilter receiveFilter;
private MessageReceiver messageReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sender=(TextView)findViewById(R.id.sender);
    content=(TextView)findViewById(R.id.content);
    receiveFilter=new IntentFilter();
    Log.v("sss","success0");
    receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    Log.v("sss","success1");
    messageReceiver=new MessageReceiver();
    Log.v("sss","success2");
    registerReceiver(messageReceiver,receiveFilter);
    Log.v("sss","success3");
}
 class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.v("sss","success4");
        Bundle bundle=intent.getExtras();//从intent中取出一个bundle对象
        Object[] pdus=(Object[])bundle.get("pdus");
        //提取短信消息使用pdus密钥提取一个SMS pdus数组;
        SmsMessage[] messages=new SmsMessage[pdus.length];

        //将每一字节数组转换为SmsMessage对象

        for(int i=0;i<messages.length;i++)
        {
            messages[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
        }
        String address=messages[0].getOriginatingAddress();
        //调用message对象的getOriginatingAddress()方法
        //获取发送方号码;
        String fullMessage=" ";
        for(SmsMessage message: messages)
        {
            fullMessage+=message.getMessageBody();//获取短信内容将短信内容拼接起来
        }
        sender.setText(address);//显示在TextView上
        content.setText(fullMessage);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    unregisterReceiver(messageReceiver);
}

}

  • 写回答

1条回答 默认 最新

  • s39014971 2016-03-13 08:35
    关注

    定义了两个Textview为什么收到短信之后程序界面没有显示

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!