私のアプリは緯度と経度を取得できるように(家から仕事に)人の軌跡を取得する必要があります(2つのボタンがあります。 2.停止して緯度と経度を取得します)。しかし、私はアンドロイドがアプリを殺さないようにしたい、ユーザーがFacebookやtwitter(例えば)を使用している間、または単にユーザーがスマートフォンをロックしている間、アプリケーションを実行しておきたい。 スマートフォンをロックすると、FacebookやTwitterなどでアプリを使用するときにアプリが正常に動作しますが、Androidはアプリを強制終了します。 intentserviceとサービスを使用しようとしましたが、画面をロックすると機能しません。 PowerManager.WakeLockを使うべきですか?私はそれがどのように機能し、何をしているのか正確には分かりません。いつもアプリをAndroidでバックグラウンドで実行し続ける
これは私が間違っているかどうかわからないサービスを試すための例ですが、次の場合には機能しません。 1.私はサービスに入っています(サービスを開始しました) 2。私はホームボタンをタップします(サービスの実行中) 3.私は画面をロックします。
<?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>
activity_main 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>
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がアプリを殺します。 –
コードを投稿できますか?サービスを試してみたが、サービスの開始方法と終了方法は重要です。サービスのライフサイクルの方法だけでなく、サービスの開始と終了のコードを投稿するとよいでしょう。私はあなたがサービスでやろうとしていることをやっているので、それを行うことができます。 – Gary99