从服务启动Intent会导致崩溃

时间:2013-03-14 15:04:27

标签: android android-service

回复:Move back to App after launching another App

我现在已经设置了一个服务来运行我的runCommand()函数(见下文),但是当从run命令中实际启动intent时,我的app force将关闭:

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

在我的主要活动中

2 个答案:

答案 0 :(得分:4)

尝试添加此标记:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

答案 1 :(得分: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);