0

回复:启动另一个应用程序后返回应用程序

我现在已经设置了一个服务来运行我的 runCommand() 函数(见下文),但是当从运行命令中实际启动意图时,我的应用程序强制关闭:

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

E/AndroidRuntime (5448): 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 (5448): at android.app.ContextImpl.startActivity(ContextImpl.java:864)

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

E/AndroidRuntime (5448): at packagename.MainService.runCommand(MainService.java:164)

所以我的问题是我将如何从服务中启动所需的意图?

我的服务等级是:

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");
                }

        }
    };

    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;
}

}

我的服务开始使用:

Intent intentService = new Intent(this, MainService.class);
    this.startService(intentService);

在我的主要活动中

4

2 回答 2

4

尝试添加此标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
于 2013-03-14T15:07:32.110 回答
0

试试这个,它会解决你的问题。

Intent mInMainAndroidAct= new Intent(getBaseContext(), myActivity.class);
mInMainAndroidAct.addCategory(Intent.CATEGORY_DEFAULT);
mInMainAndroidAct.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mInMainAndroidAct.putExtra("jackpal.androidterm.iInitialCommand", command);
mInMainAndroidAct.putExtra("jackpal.androidterm.window_handle", MainActivity.mHandle);
getApplication().startActivity(mInMainAndroidAct);
于 2013-03-14T15:18:20.927 回答