从服务意图启动已经运行的活动

时间:2013-03-14 16:29:26

标签: android android-service

RE:Move back to App after launching another AppLaunching Intent from service causes crash

我现在已经从我的服务正确启动了终端,但是我的下一步是在服务启动终端意图重新启动应用程序主要活动之后。

我这样做是使用:

public void backtoEmplayer(){
    Intent intenti = new Intent(MainService.this,MainActivity.class);
    intenti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intenti.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intenti);
}

因此它只会提出仍应运行的主要活动(因为在此设备上启动的唯一两件事将是我的应用程序和终端)。 但是,当我运行上面的代码时,我收到以下错误:

E/AndroidRuntime (1859): FATAL EXCEPTION: Thread-931

E/AndroidRuntime (1859): android.util.AndroidRuntimeException: Calling startActvitiy() from outside of an Activity context requires the FLAT_ACTIVITY_NEW_TASK flas. Is this really what you want?

E/AndroidRuntime (1859): at android.app.ContextImpl.startActivity(ContextImpl.java:864)

E/AndroidRuntime (1859): at android.content.ContentWrapper.startActivity(ContextWrapper.java 276)

E/AndroidRuntime (1859): at packagename.MainService.backtoEmplayer(MainService.java:187)

我不明白的事实是我使用的是FLAT_ACTIVITY_NEW_TASK标志,但它仍然不起作用。

我的完整服务类现在看起来像:

public class MainService extends Service {

    boolean copied = false;
    private String mHandle;
    private static final int REQUEST_WINDOW_HANDLE = 1;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onStart(Intent intent, int startId) {
        Thread usbUpdateThread = new Thread() {
            public void run() {             
                while (true) {
                    while (!copied) {
                        try {
                            Thread.sleep(180000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    runCommand("chmod -R 777 /media/cdrom/");
                    copied = false; 
                }
            }
        };
        Thread InternetThread = new Thread() {
            public void run() {
                while (true){
                        try {
                            Thread.sleep(3600000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        runCommand("php update.php");

                }
            }
        };
        Thread CheckThread = new Thread() {
            public void run() {
                while (true) {
                        try {
                            Thread.sleep(300000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        runCommand("sh check.sh");
try {


Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    backtoEmplayer();
                    }

            }
        };

        InternetThread.start();
        CheckThread.start();
        usbUpdateThread.start();
    }


    public boolean runCommand(String command) {
        Intent intent = new Intent("jackpal.androidterm.RUN_SCRIPT");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("jackpal.androidterm.iInitialCommand", command);
        intent.putExtra("jackpal.androidterm.window_handle", MainActivity.mHandle);
        startActivity(intent);


        return true;
    }
    public void backtoEmplayer(){


        Intent intenti = new Intent(MainService.this,MainActivity.class);
        intenti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intenti.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intenti);
    }

}

1 个答案:

答案 0 :(得分:-1)

删除

intenti.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  

来自你的代码。