我知道這已被問過一百萬次,但沒有任何工作對我來說。我在一個單獨的課程中有一項服務,在從LaunchIntent
啓動應用程序後,按下按鈕時需要啓動該服務。按下按鈕時服務不啓動?
長話短說,這裏就是我的目標:
運行命令>等待三秒鐘的命令來運行>啓動應用程序>啓動服務
的服務是監測的CONFIGURATION_CHANGED廣播。
清單(即重要的部分):
</activity>
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED" >
</action>
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
MyService.java:
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.app.Service;
public class MyService extends Service {
String[] commandsdefault = {"/x"};
public void onCreate() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
public void onDestroy() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
public void onReceive(Context context, Intent intent) {
MainActivity ogres = new MainActivity();
ogres.RunAsRoot(commandsdefault);
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
然後,我只是在我的MainActivity.java以下行來調用服務啓動:
startService(new Intent(getApplicationContext(), MyService.class));
我比在模特店的蚊子更困惑。除了u=0 not found
之外,LogCat完全沒有任何幫助。
這裏有什麼不對嗎?我甚至從服務開始就看不到敬酒。
顯示代碼如何在按鈕單擊事件中啓動服務 –
如果您指定了所嘗試的內容並特別指出您嘗試過的SO問題中的其他答案,可能會有所幫助。 :) –