Hai嘗試使用service class.my服務函數獲取緯度和經度值。但我無法啓動gpsclass.am的活動,正在獲取force close.i加入清單文件也無法從Android的服務類中啓動Activity
清單文件中的類
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javaorigin.android.sample.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application icon="@drawable/icon" label="@string/app_name">
<service class=".MyService" android:name=".MyService">
<intent-filter>
<action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
android:name=".MyService" />
</intent-filter>
</service>
<activity android:name=".SampleAction"
android:label="@string/app_name">
<activity android:name=".getLocation"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
</manifest>
logcat的消息
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=com.javaorigin.android.sample.service/.MyService }: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.javaorigin.android.sample.service/com.javaorigin.android.sample.service.getLocation}; have you declared this activity in your AndroidManifest.xml?
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.os.Looper.loop(Looper.java:123)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at java.lang.reflect.Method.invoke(Method.java:521)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at dalvik.system.NativeStart.main(Native Method)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.javaorigin.android.sample.service/com.javaorigin.android.sample.service.getLocation}; have you declared this activity in your AndroidManifest.xml?
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ContextImpl.startActivity(ContextImpl.java:622)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at com.javaorigin.android.sample.service.MyService.onStart(MyService.java:25)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.Service.onStartCommand(Service.java:420)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
01-12 14:12:08.911: ERROR/AndroidRuntime(4071): ... 10 more
我的代碼
public class MyService extends Service {
String tag="TestService";
private Intent Information;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
Log.i(tag, "Service created...");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Intent i=new Intent(getBaseContext(),getLocation.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(i); Log.i(tag, "Service started...");
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
Intent i = new Intent(getBaseContext(),getLocation.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(i);我也試過這個 – Mercy 2012-01-12 08:47:29
更新了以前的帖子。 – JoxTraex 2012-01-12 08:49:54
logcat messge? – Mercy 2012-01-12 08:51:14