我在活动中有一个按钮。通过点击它,发送具有预定义的身体和电话地址的SMS,然后包括代码的回复SMS被自动发送到我的移动电话。我需要收到代码并将其设置为我的textview。我该如何解决?我有两个活动:MainActivity和MessageReceiver。
//MainActivity.java
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView chargeText = (TextView) findViewById(R.id.chargeText);
Log.d("myTag", "is chargeText null? : " + (chargeText==null));
Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));
chargeText.setText(intent.getExtras().getString("message"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
button3 = (Button) findViewById(R.id.button);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mymsg = "CMDACC_1234";
String thenumber = "09380638202";
SendChargeMessage(thenumber, mymsg);
}
});}
public void SendChargeMessage(String thenumber, String mymsg) {
String SENT = "Message sent";
String DELIVERED = "Message delivered";
SmsManager smsManager = SmsManager.getDefault();
Context curContext = this.getApplicationContext();
PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
0, new Intent("SENT"), 0);
curContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sent.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SENT"));
PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
0, new Intent("DELIVERED"), 0);
curContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Delivered.",
Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("DELIVERED"));
PackageManager pm = this.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
} else {
smsManager.sendTextMessage("09380638202", null, "CMDACC_1234", sentPendin
g, deliveredPending);
//chargeText.setText(SMSBody1);
}
}
///Edited
@Override
protected void onResume(){
super.onResume();
IntentFilter intentFilter;
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView chargeText = (TextView) findViewById(R.id.chargeText);
Log.d("myTag", "is chargeText null? : " + (chargeText==null));
Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));
String text = intent.getExtras().getString("message").toString(); // let's try this with toString() so we are very explicit about it
Log.d("myTag", "The converted text is: " + text);
chargeText.setText(text);
}
};
registerReceiver(intentReceiver , intentFilter);
} }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dearzeinab.emaapplication">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.dearzeinab.emaapplication.MessageReceiver" android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
manifest.xml
setValue()
答案 0 :(得分:0)
您需要注册接收器。
像这样:
getActivity().registerReceiver(intentReceiver, new IntentFilter("SMS_RECIEVED_ACTION"));
只有这样才能奏效。
https://developer.android.com/reference/android/content/BroadcastReceiver
https://developer.android.com/guide/components/broadcasts
以下是代码的外观:
//MainActivity.java
private BroadcastReceiver intentReceiver; // do not set it up here. We'll set it and register it onCreate(), but we'll keep it here so it stays on scope of every other function
private IntentFilter intentFilter; // same for the filter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// We'll set up the receiver here, after the activity starts
intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView chargeText = (TextView) findViewById(R.id.chargeText);
Log.d("myTag", "is chargeText null? : " + (chargeText==null));
Log.d("myTag", "The text is: " + (intent.getExtras().getString("message")));
String text = intent.getExtras().getString("message").toString(); // let's try this with toString() so we are very explicit about it
Log.d("myTag", "The converted text is: " + text);
chargeText.setText(text);
}
};
// then, we'll create the filter
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
button3 = (Button) findViewById(R.id.button);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String mymsg = "CMDACC_1234";
String thenumber = "09380638202";
SendChargeMessage(thenumber, mymsg);
}
});
registerReceiver(intentReceiver , intentFilter); // registering
}
public void SendChargeMessage(String thenumber, String mymsg) {
String SENT = "Message sent";
String DELIVERED = "Message delivered";
SmsManager smsManager = SmsManager.getDefault();
Context curContext = this.getApplicationContext();
PendingIntent sentPending = PendingIntent.getBroadcast(curContext, // Are you sure you want to create a new intent here?
0, new Intent("SENT"), 0);
curContext.registerReceiver(new BroadcastReceiver() { // and are you sure this is supposed to be a new receiver as well? Are you registering it like we did with iontentReceiver?
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sent.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("SENT"));
PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
0, new Intent("DELIVERED"), 0);
curContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Delivered.",
Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter("DELIVERED"));
PackageManager pm = this.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
} else {
smsManager.sendTextMessage("09380638202", null, "CMDACC_1234", sentPending, deliveredPending);
//chargeText.setText(SMSBody1);
}
}
我在您的代码中看到的内容:
您在onStart()之前注册了一个接收器。这个接收器适用于每种方法。但它没有注册。
您在onResume()中创建了一个新接收器,其名称与前一个接收器相同(broadcastReceiver)。您正在正确注册这个,但它超出了每个功能的范围。
这意味着您正在尝试访问未注册的接收器,并且您正在注册具有相同名称但超出范围的接收器。
如果这不起作用,至少你会更接近它的工作;)
如果您需要我的意见,您可以:
研究范围及其在面向对象编程中的工作原理;
研究接收器,意图以及如何注册它们。
希望它有所帮助。 祝你好运。
答案 1 :(得分:-1)
在接收器中使用以下代码
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
String senderAddress = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Log.d(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);
// if the SMS is not from our gateway, ignore the message
if (!senderAddress.toLowerCase().contains(GlobalApplication.SMS_ORIGIN.toLowerCase())) {
return;
}
// verification code from sms
String verificationCode = getVerificationCode(message);
Log.d(TAG, "OTP received: " + verificationCode);
mListener.otpReceived(verificationCode);
}
}
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
/**
* Getting the OTP from sms message body
* ':' is the separator of OTP from the message
*
* @param message
* @return
*/
private String getVerificationCode(String message) {
String str_msg = message.replace("Dear Customer,","");
str_msg = str_msg.replace("is your one time password (OTP). Please enter the OTP to proceed.","");
str_msg = str_msg.replace("Thank you,","");
str_msg = str_msg.replace("ChannelPaisa","");
str_msg = str_msg.replace("\n","");
str_msg= str_msg.trim().toString();
return str_msg;
}