2

I have a task in which I have to send E-Mail each day once. I used Service triggered by AlarmManager to achieve this. It's working properly. But the problem is the mail gets send only if the mobile data is available. So I tried to turn on the data connection and send mail. Mobile data is enabled but, Mail not sending. I have posted here what I've tried. Someone please suggest a method to wait until the user turn on mobile data and send mail. Thank you.

cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        ni=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        isConnected=ni!=null&&ni.isConnected();

        Mail m=new Mail("xxxxxxxxx@gmail.com","xxxxxxx");
        String[] strTo={"xxxxxxxxx@gmail.com"};
        m.setTo(strTo);
        m.setFrom("xxxxxxxxxx@gmail.com");
        m.setSubject("Subject");
        m.setBody("Please find the attachment");

        try{
            m.addAttachment(Environment.getExternalStorageDirectory() + "/xxxxx/xxxxxx.xx");
            if (isConnected){
                m.send();
            }else {
                ConnectivityManager dataManager;
                dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                Method dataMtd = null;
                try {
                    dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                dataMtd.setAccessible(true);
                try {
                    dataMtd.invoke(dataManager, true);
                } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
                NetworkInfo netInfo=dataManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                boolean isOnline=netInfo!=null&&netInfo.isConnected();
                if(isOnline){
                    if (m.send()){
                        ConnectivityManager dataManager1;
                        dataManager1  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                        Method dataMtd1 = null;
                        try {
                            dataMtd1 = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                        dataMtd1.setAccessible(false);
                        try {
                            dataMtd1.invoke(dataManager1, false);
                        } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }catch (final Exception e){
            e.printStackTrace();
            Log.v("My_Service",e.toString());
        }
Praveen Kumar
  • 547
  • 1
  • 7
  • 33

3 Answers3

1

You need to have BroadcastReceiver listening to change in data connectivity. Refer to - http://developer.android.com/reference/android/content/BroadcastReceiver.html

Check this for reference specific to network connectivity - Check INTENT internet connection

Community
  • 1
  • 1
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
1

Don't relay on Google shit api... Info to the user to enable data + post delayed eg time task :) u can not handle all problems by enabling data.. But u can check for most : There could by a firewall issue , DNS issue, TTL issue when theter etc

ceph3us
  • 7,326
  • 3
  • 36
  • 43
0

You can register a BroadcastReceiver to be notified when a Mobile data is enabled.

Register the BroadcastReceiver:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(broadcastReceiver, intentFilter);

And then in your BroadcastReceiver do something like this:

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION))) {
        if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)){
            // data connection was lost
        } else {
            // do your stuff
        }
    }
}

For more info, see the documentation for BroadcastReceiver

You can use below link to send mail :

How to send mail without user interaction from another application

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • I tried to send mail as soon as enabled the data. It caused the error. And now I've added 5 minutes time delay to send mail after enabling data. And now it's working fine. – Praveen Kumar Oct 07 '15 at 04:47
  • @PraveenKumar - u should use operation helper - do with retry for internet connections (as there also could be soex timeout) - I do not agree with the proposed solution - What if the connection will be lost during the send??? – ceph3us Oct 07 '15 at 13:51
  • @Tomasz Best i can understand your concern but can you please give some example of doing that ? – KishuDroid Oct 07 '15 at 13:56
  • @KishuDroid - u mean do with retry ? – ceph3us Oct 07 '15 at 14:01
  • See this answer is for detect mobile data enabled and send email. Now there are the test cases if network lost or phone gets hang or phone gets reboot.So for its question perspective this is proper answer. – KishuDroid Oct 07 '15 at 14:04
  • @KishuDroid - learn to read careful - "So I tried to turn on the data connection and send mail. Mobile data is enabled but, Mail not sending..." - if u think that enabling data connection is enought to send something then still u must learn many things – ceph3us Oct 07 '15 at 14:09