2

As per the client purpose, I have to make an app of retrieving message data in background and send to his email. Actually I can't get any idea how to store the retrieved data and how to send it on provided email. I have made a main class having email field and an another class of retrieving message data and a service class . please suggest how can I do it.....

4

2 回答 2

0

您需要将要发送的数据写入 SD 卡上的文件。这是您如何格式化您想要做的事情的意图(来自我用来通过电子邮件发送我已经写入 SD 卡的 XML 文件的代码):

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, emailID)      // The string that has the email ID you are sending to
i.putExtra(Intent.EXTRA_STREAM, fileURI);    // The URI of the file on the SD card
startActivity(Intent.createChooser(i, "Email:"));

这是我自己学的。出于安全原因,您必须将要附加到 SD 卡上的电子邮件的文件放在公共场所。

于 2013-10-30T07:14:01.413 回答
0

使用此代码,它会在收到任何短信并将电子邮件发送到特定邮件 ID 时触发:

主要活动:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        Intent intent= new Intent(MainActivity.this, SmsApp.class);
    sendBroadcast(intent);
}

}

广播接收器:

 public class SmsApp extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String TAG = "SMSBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    if (intent.getAction()
            .equals("android.provider.Telephony.SMS_RECEIVED"))
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            if (messages.length > -1) {
                 Intent intent2 = new Intent(context,SecondActivity.class);
                            context.startActivity(intent2);
                intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                /*intent2.putExtra("sender",
                        messages[0].getOriginatingAddress());*/
                intent2.putExtra("message", messages[0].getMessageBody());
                context.startActivity(intent2);
            }

        }

            }
        }
    }
}
}

第二活动:

 public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String sender = bundle.getString("sender");
    String message = bundle.getString("message");
    Intent intent2 = new Intent(Intent.ACTION_SEND);
            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent2.setType("message/rfc822");
    intent2.putExtra(Intent.EXTRA_EMAIL,
            new String[] { "recipient@example.com" });
    intent2.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
    intent2.putExtra(Intent.EXTRA_TEXT,
            message);
    try {
        startActivity(Intent.createChooser(intent2,
                "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        System.out
                .println("There are no email clients installed.");
    }
}
}

在清单文件中添加权限:

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
于 2013-10-30T05:58:36.277 回答