簡體   English   中英

無法從Android中的服務類啟動活動

[英]unable to start the Activity from the service class in Android

Hai嘗試使用服務類獲取緯度和經度值。我的服務功能運行正常。但是我無法啟動gpsclass.am的活動,並強制關閉。我還在清單文件中添加了該類

清單文件

<?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 com.javaorigin.android.sample.service.MyService@44ee3168 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;
 }

認真...?

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?
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文件中聲明您的活動。

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

應用此代碼,這將啟動您的接收器

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

您是否嘗試過傳遞baseContext以外的其他上下文? 我覺得我記得上下文可能不是您想要的,例如嘗試傳遞applicationContext

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

代替

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM