Sankar Ganesh PMP
Sankar Ganesh PMP

Reputation: 12037

Problem in Sending Email in Android?

I had used the Intent code for sending Email , I was able to send the email Successfully, but my problem is if the user click the Gmail App and open Inbox click a email and press Home Key , then open my app and click Email Button it opens the Inbox rather than the composing page ( i.e , it would go to whatever the last view I had been on in Gmail, either inbox, or a message thread, or even a draft message ) so i planned to kill the background Gmail Process but Mr.Commonsware has mentioned in his answer that we cannot kill another app from our application so i try to kill the Back ground process and also restart package all of my efforts are went in vain.

Here's my Email Sending code

public static ComponentName name = null;

    name = new ComponentName(
            "com.android.email",
            "com.android.email.activity.MessageCompose");

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        intent.setComponent(name);
        intent.setType("application/octet-stream");
        intent.putExtra(Intent.EXTRA_EMAIL,
                new String[] { " " });
        intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
        intent.putExtra(
                Intent.EXTRA_TEXT,
                Html.fromHtml("<b> test Message</b>"));
        intent.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file:///" + "sdcard/download/" + imgfilename));  

        startActivity(intent);

here is my restartPackage code

            ActivityManager aM = (ActivityManager);
getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
            aM.restartPackage("com.android.email");

Here is my Kill BackGround Process Code

                        ActivityManager aM = (ActivityManager)
        getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
                        aM.killBackgroundProcesses("com.android.email");

Here is my code which fetches all running Application and check wether email app is already running or not , if it is running then kill that process

    ActivityManager manager =  (ActivityManager);    getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> activityes = ((ActivityManager) manager).getRunningAppProcesses();

            for (int iCnt = 0; iCnt < activityes.size(); iCnt++){

                System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);

if (activityes.get(iCnt).processName.contains("com.android.email")){
                    android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
                     android.os.Process.killProcess(activityes.get(i).pid);
                    //manager.killBackgroundProcesses("com.android.email");

//manager.restartPackage("com.android.email");  

                     System.out.println("Inside if");
                }

            }

Upvotes: 2

Views: 1114

Answers (2)

Sankar Ganesh PMP
Sankar Ganesh PMP

Reputation: 12037

It was fixed by just removing this Flag and added the following flags

         intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK 
            | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | 
         Intent.FLAG_ACTIVITY_CLEAR_TOP 
| Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY 
    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
            Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

Upvotes: 2

pawelzieba
pawelzieba

Reputation: 16082

Try:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
            | Intent.FLAG_ACTIVITY_CLEAR_TASK );

Upvotes: 1

Related Questions