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;
}
答案 0 :(得分: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?
答案 1 :(得分: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
文件中声明您的活动。
答案 2 :(得分:0)
Intent myIntent = new Intent(getBaseContext(), myActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
答案 3 :(得分:0)
应用此代码,这将启动您的Receiver
Intent myIntent = new Intent ( context, getLocation.class );
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
答案 4 :(得分:-1)
您是否尝试过除baseContext之外的其他上下文?我觉得我记得上下文可能不是你想要的,尝试传递applicationContext例如
Intent i=new Intent(getApplicationContext(),getLocation.class);
而不是
Intent i=new Intent(getBaseContext(),getLocation.class);