回覆:Move back to App after launching another App從服務啓動意圖導致崩潰
我現在已經設立了服務但是,當涉及到實際發射運行命令我的應用程序部隊內的意圖來運行我的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);
在我的主要活動
貼logcat的一個更大的詳細 – stinepike 2013-03-14 15:06:32
我不好會做:) – 2013-03-14 15:06:53
您可以發佈完整的堆棧跟蹤? – MCeley 2013-03-14 15:06:54