在android中发送短信时显示活动

时间:2014-04-03 11:18:10

标签: java android smsmanager

我正在开发Android应用程序。在我的应用程序中我需要发送短信给收件人。如果短信发送给收件人它将显示吐司消息并显示新活动。我确实使用SMSManager类但我不想使用SMSManager我的应用程序。我想使用发送短信的意图,它给我发送消息的默认短信视图。我使用了成功发送短信但无法调用其他活动的意图。请解决我的问题,我做了很多R& D但没有成功。

我附上发送短信的源代码并调用活动

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();
}

1 个答案:

答案 0 :(得分:1)

您可以使用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()并捕获该请求代码(在启动活动中自定义)。这就是它的样子:

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