根据客户的目的,我必须制作一个应用程序,在后台检索邮件数据并发送到他的电子邮件。 实际上我无法知道如何存储检索到的数据以及如何在提供的电子邮件中发送它。 我创建了一个具有电子邮件字段的主类和另一类检索消息数据和服务类。 请建议我该怎么办.....
答案 0 :(得分:0)
使用此代码,它会在收到任何短信时触发,并向特定邮件ID发送电子邮件:
MainActivity:
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);
}
}
}
}
}
}
}
SecondActivty:
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" />
答案 1 :(得分: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卡上的电子邮件中。