gpt4 book ai didi

java - 在android中发送短信时显示 Activity

转载 作者:行者123 更新时间:2023-12-01 13:11:24 25 4
gpt4 key购买 nike

我正在开发 Android 应用程序。在我的应用程序中,我需要向收件人发送短信。如果向收件人发送短信,它将显示 toast 消息并显示新 Activity 。我使用了 SMSManager 类,但我不想在中使用 SMSManager我的应用程序。我想使用发送短信的 Intent ,这为我提供了用于发送消息的默认短信 View 。我使用 Intent 成功发送短信但无法调用其他 Activity 。请解决我的这个问题我做了很多研发但没有成功。

我附上了发送短信和调用 Activity 的源代码

try {
String smsSent = "SMS_SENT";
String smsDelivered = "SMS_DELIVERED";

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(smsSent), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(smsDelivered), 0);

// Receiver for Sent SMS.
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
Intent i=new Intent(this,Display.class).startActivity(i);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(smsSent));

// Receiver for Delivered SMS.
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(smsDelivered));


/*SmsManager smsman=SmsManager.getDefault();
smsman.sendTextMessage(no, null, data, null, null);*/

/*
// called new activity here
Intent i=new Intent(MainActivity.this,Display.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "SMS sent",Toast.LENGTH_LONG).show();
*/


Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "");
smsIntent.putExtra("sms_body","");
startActivity(smsIntent);
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}

最佳答案

您可以使用onActivityResult方法。

编辑您的代码如下:

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "");
smsIntent.putExtra("sms_body","");
startActivityForResult(smsIntent, MY_REQUEST_CODE);//MY_REQUEST_CODE int number

然后您可以重写 onActivityResult() 并在那里捕获该请求代码(在启动 Activity 中自行定义)。它看起来像这样:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// do something useful
}
}
}

关于java - 在android中发送短信时显示 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835956/

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