Android
 Computer >> コンピューター >  >> プログラミング >> Android

常にバックグラウンドでAndroidサービスを実行するにはどうすればよいですか?


この例は、Androidサービスを常にバックグラウンドで実行する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <Button
      android:id="@+id/button"
      android:text="Click here to start background Service"
      android:textStyle="bold"
      android:textSize="16sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true" />
</RelativeLayout>

ステップ3 –プロジェクトを右クリックし、[新規]>>[サービス]>>[サービス]を選択して、以下をMyServices.javaに追加します

package app.com.sample;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
   public MyService() {
   }
   @Override
   public int onStartCommand(Intent intent, int flags, int startId){
      onTaskRemoved(intent);
      Toast.makeText(getApplicationContext(),"This is a Service running in Background",
      Toast.LENGTH_SHORT).show();
      return START_STICKY;
   }
   @Override
   public IBinder onBind(Intent intent) {
      // TODO: Return the communication channel to the service.
      throw new UnsupportedOperationException("Not yet implemented");
   }
   @Override
   public void onTaskRemoved(Intent rootIntent) {
      Intent restartServiceIntent = new Intent(getApplicationContext(),this.getClass());
      restartServiceIntent.setPackage(getPackageName());
      startService(restartServiceIntent);
      super.onTaskRemoved(rootIntent);
   }
}

ステップ4 −次のコードをsrc / MainActivity.java

に追加します
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 {
   Button button;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button = findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            startService(new Intent(getApplicationContext(),MyService.class));
         }
      });
   }
}

ステップ5 -次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <service
         android:name=".MyService"
         android:enabled="true"
         android:exported="true"></service>
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–

常にバックグラウンドでAndroidサービスを実行するにはどうすればよいですか?

常にバックグラウンドでAndroidサービスを実行するにはどうすればよいですか?


  1. AndroidデバイスでLinuxを実行する方法

    これを読んでいるなら、おそらく以前にLinuxを使用したことがあり、ほとんどすべてのハードウェアで動作することを知っています。その間、あなたはあなたのポケットの中に電話を持っています、そしてあなたはそれが用途が広いことを知っています。では、AndroidフォンでLinuxを実行できますか? はい、できます。 AndroidスマートフォンまたはタブレットでLinuxを実行するために知っておくべきことは次のとおりです。 待ってください、AndroidはすでにLinuxではありませんか? ええと、そうです...そしてそうではありません。 Androidは、デバイスのハードウェアとインターフェイ

  2. Windows11で常にバックグラウンドでWSAを実行する方法

    Windows Subsystem for Android(WSA)をバックグラウンドで実行したい場合 Windows 11では、この記事でプロセスを説明します。 Androidアプリをすばやく開くことができるように、Windows11でAndroid用のWindowsサブシステムを常にバックグラウンドで実行することができます。 Android用Windowsサブシステムを使用すると、Windows 11でAndroidアプリを実行できます。コンピューターでアプリを実行すると、仮想マシンと他のすべてのプロセスがバックグラウンドで実行されます。プロセス全体に時間がかかるため、コンピューターでA