如何通过执行流程在Android上发送电子邮件

时间:2012-11-06 22:18:05

标签: android android-intent adb

我正在努力从应用内添加对Android shell的调用。我已经能够很好地调用shell脚本或基本的shell命令,但现在我正在尝试发送一封电子邮件(主要是为了知道我是否可以)并且遇到了一个奇怪的错误:

11-06 16:14:43.449: D/AndroidRuntime(28655): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-06 16:14:43.449: D/AndroidRuntime(28655): CheckJNI is OFF
11-06 16:14:43.629: D/AndroidRuntime(28655): Calling main entry com.android.commands.am.Am
11-06 16:14:43.639: D/AndroidRuntime(28655): Shutting down VM
11-06 16:14:43.639: I/ActivityManager(204): START {act=android.intent.action.SENDTO typ="text/plain" flg=0x10000000 pkg=Goes (has extras)} from pid 28655
11-06 16:14:43.649: D/dalvikvm(28655): GC_CONCURRENT freed 104K, 81% free 495K/2560K, paused 0ms+1ms
11-06 16:14:43.649: D/dalvikvm(28655): Debugger has detached; object registry had 1 entries
11-06 16:14:43.649: I/AndroidRuntime(28655): NOTE: attach of thread 'Binder Thread #2' failed

我的shell运行的代码是:

am start -a android.intent.action.SENDTO -t "text/plain" --es android.intent.extra.EMAIL "myaddress@example.com" --es android.intent.extra.TEXT "Message Goes here" --es android.intent.extra.SUBJECT "this is the subject"

出了什么问题,我该如何解决?我应该使用不同的工具(am除外)吗?

3 个答案:

答案 0 :(得分:1)

如果你在adb shell中,请试试这个:

am start -a android.intent.action.SENDTO -d sms:xxx --es sms_body "xxx" --ez exit_on_sent true
input keyevent 66

我刚尝试运行Android 4.0(ICS)的Droid X.它会发送短信并退出。如果有帮助,请告诉我。

此致

答案 1 :(得分:1)

尝试使用此代码发送电子邮件。在真实设备中运行此代码,因为adb没有任何第三方权限

package com.rmn.emailSending;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EmailSendingActivity extends Activity {
    Button send;
    EditText address, subject, emailtext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        send = (Button) findViewById(R.id.emailsendbutton);
        address = (EditText) findViewById(R.id.emailaddress);
        subject = (EditText) findViewById(R.id.emailsubject);
        emailtext = (EditText) findViewById(R.id.emailtext);

        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

                emailIntent.setType("message/rfc822");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { address.getText().toString() });
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject.getText().toString());
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,emailtext.getText().toString());
                EmailSendingActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail in"));

            }
        });
    }
}

答案 2 :(得分:1)

经过多次尝试后,我开始相信您无法从am设备上启动活动。但是,您可以发送可用于启动活动的广播。例如,要启动相机应用程序,您可以使用:

am broadcast -a android.intent.action.CAMERA_BUTTON