我必须获取人员的轨迹(例如从家到工作),因此我的应用程序获取了经度和纬度(我有两个按钮:1.开始获取纬度和经度2.停下来获取经纬度)。但是,我希望Android不会杀死应用程序,我想让应用程序在用户使用Facebook,Twitter(例如)时运行,或者只是用户锁定他的智能手机。 当用户在使用Facebook或Twitter时(例如)使用应用程序时,我的应用程序工作正常,但是当我锁定我的智能手机时,Android会终止应用程序。 我试图使用intentservice和服务,但他们不工作时,我锁定屏幕。我应该使用PowerManager.WakeLock吗?我不完全知道它是如何工作的以及它是如何工作的。保持应用程序始终在Android上运行在后台
这是我做了尝试服务,我不知道如果我错了一个例子,但是当它不工作: 1.我在应用程序(我已经开始服务) 2。我点击主页按钮(服务运行时) 3.我锁定屏幕。 4.然后,服务和应用程序是由机器人杀死(和服务没有完成做它的东西)
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javosoft.intentservice">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:exported="false"></service>
</application>
</manifest>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.javosoft.intentservice.MainActivity">
<Button
android:text="start Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:id="@+id/button"
android:onClick="startService"/>
<Button
android:text="Stop Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginBottom="68dp"
android:id="@+id/button2"
android:onClick="stopService" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="textito :D"
android:ems="10"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/editText" />
</RelativeLayout>
MainActivity
package com.javosoft.intentservice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
}
public void stopService (View view){
Intent intent = new Intent(this, MyService.class);
stopService(intent);
}
}
为MyService类
package com.javosoft.intentservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
final class MyThreadClass implements Runnable{
int service_id;
MyThreadClass(int service_id){
this.service_id = service_id;
}
@Override
public void run() {
synchronized (this){
int count = 0;
while (count < 10){
Log.i("servicio", "while: " + String.valueOf(count));
try {
wait(2000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
stopSelf(service_id);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "El servició ha empezado D:", Toast.LENGTH_SHORT).show();
Log.i("servicio", "El servició ha empezado D:");
Log.i("servicio", "onStartCommand arriba");
Thread thread = new Thread(new MyThreadClass(startId));
thread.start();
Log.i("servicio", "onStartCommand abajo");
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
//super.onDestroy();
Toast.makeText(this, "El servicio ha padarado ._.", Toast.LENGTH_SHORT).show();
Log.i("servicio", "El servicio ha padarado ._.");
}
//@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
感谢
呃,服务应该在关闭屏幕时起作用。 – Gudin
它可以工作,如果我在应用程序,然后我锁定屏幕,但如果我在应用程序中,我点击主页按钮,然后锁定屏幕,Android杀死应用程序。 –
你可以发布你的代码吗?你说你已经尝试了服务,但是你如何开始和结束服务很重要。发布开始和结束服务的代码以及Service的生命周期方法是很好的。我正在做你想用服务做什么,这样就可以完成。 – Gary99