gpt4 book ai didi

android - 在 TextView 中显示短信

转载 作者:行者123 更新时间:2023-11-29 21:30:42 25 4
gpt4 key购买 nike

<分区>

我正在编写一个小应用程序,当手机收到短信时。它将在 TextView 中显示发件人电话号码和 SMS 正文。我有一个 SMS BoardcastReceiver 和一个 Activity

这是我的短信监听器。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver {

// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
BroadcastNewSms ourSMS;

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
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("SmsReciver", "senderNum: " + senderNum
+ ", message: " + message);
//ourSMS.getSmsDetails(senderNum, message);
// Show SMS notification
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();

} // end of for loop
} // bundle

} catch (Exception e) {
// TODO: handle exception
Log.e("SmsReciver", "Exception smsReciver" + e);
}
}
}

这是我的 Activity :

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class BroadcastNewSms extends Activity {

TextView SMSm;
String phoneNumber1;
String SMSBody1;

public void getSmsDetails(String phoneNumber, String SMSBody) {
phoneNumber1 = phoneNumber;
SMSBody1 = SMSBody;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SMSm = (TextView) findViewById(R.id.etSmsBody);

SMSm.setText("Phone Number: " + phoneNumber1 + " " + "SMS: " +
SMSBody1);

}

}

这是我的 list :

 <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="kobi.avshalom.recivesms.BroadcastNewSms"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name="kobi.avshalom.recivesms.IncomingSms" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com