2012-01-12 64 views
0

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

回答

0

應用此代碼,這將啓動接收機

 Intent myIntent = new Intent (context, getLocation.class); 
     myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(myIntent); 
0

嚴重......?

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
+0

Intent i = new Intent(getBaseContext(),getLocation.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.startActivity(i);我也試過這個 – Mercy 2012-01-12 08:47:29

+0

更新了以前的帖子。 – JoxTraex 2012-01-12 08:49:54

+0

logcat messge? – Mercy 2012-01-12 08:51:14

0
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 

中聲明你的manifest文件的活動。

+0

我ADDED在清單也應該更新我的清單文件 – Mercy 2012-01-12 09:11:37

+0

請顯示您的清單... – 2012-01-12 09:14:25

+0

我添加了我的清單文件 – Mercy 2012-01-12 09:22:37

-1

您是否嘗試過除baseContext之外傳遞不同的上下文?我覺得我記得這方面可能不是你想要的東西,嘗試通過ApplicationContext以例如

Intent i=new Intent(getApplicationContext(),getLocation.class); 

,而不是

Intent i=new Intent(getBaseContext(),getLocation.class); 
+0

同樣的力量close.i認爲問題是清單file.Hill我現在更新? – Mercy 2012-01-12 09:14:28

+0

是啊,如果你沒有它在清單中,那肯定是問題 – MikeIsrael 2012-01-12 09:19:53

+0

我加了我的清單文件 – Mercy 2012-01-12 09:22:43

0
Intent myIntent = new Intent(getBaseContext(), myActivity.class); 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    getApplication().startActivity(myIntent);