Android 起動時にサービスを開始する
Android OS の起動時にサービスを自動的に開始するには、正しい構成を実装することが重要です。一見すべてが適切に設定されているように見えますが、目的の動作を妨げている根本的な問題がある可能性があります。
Android マニフェスト
提供されている Android マニフェストを確認してください:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phx.batterylogger" android:versioncode="1" android:versionname="1.0" android:installlocation="internalOnly"> <uses-sdk android:minsdkversion="8"></uses-sdk> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name=".BatteryLogger"></service> <receiver android:name=".StartupIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver> </application> </manifest>
BOOT_COMPLETED アクションがIntentReceiver.
BroadcastReceiver for Startup
次に、StartupIntentReceiver を確認します。
package com.phx.batterylogger; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class StartupIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, BatteryLogger.class); context.startService(serviceIntent); } }
このレシーバーは BOOT_COMPLETED アクションをリッスンし、望ましいservice.
トラブルシューティング
起動時にサービスが開始しない場合は、次の手順を検討してください。
- ログに次のことが示されていることを確認します。受信機は BOOT_COMPLETED ブロードキャストを受信しています。
- ターゲット サービスが適切に宣言され、構成されていることを確認してください
- ターゲット サービスがシステムによって無効化または最適化されていないことを確認してください。
- Context.startForegroundService(Intenttent, int notificationId) API を使用して、マニフェストでサービスを開始することを検討してください。フォアグラウンド (API レベル 26 以降で利用可能)。
Complete例
AutoStart アプリケーションの包括的な例については、次のコードを参照してください。スニペット:
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.saltriver" android:versioncode="1" android:versionname="1.0"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".autostart"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"></action> </intent-filter> </receiver> <activity android:name=".hello"></activity> <service android:enabled="true" android:name=".service"></service> </application> </manifest>
autostart.java
public class autostart extends BroadcastReceiver { public void onReceive(Context context, Intent arg1) { Intent intent = new Intent(context,service.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } Log.i("Autostart", "started"); } }
service.java
public class service extends Service { private static final String TAG = "MyService"; @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public void onStart(Intent intent, int startid) { Intent intents = new Intent(getBaseContext(),hello.class); intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intents); Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); } }
hello.java
public class hello extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show(); } }
実装これらの手順を徹底的に行うことで、OS 起動時に Android サービスが正常に開始されるようになります。
以上がAndroid サービスが起動時に開始しないのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

jvm'sperformanceiscompetitivewitherruntimes、sped、safety、andproductivityの提供

javaachievesplatformedentenceTheThejavavirtualMachine(JVM)、avainwithcodetorunonanyplatformwithajvm.1)codescompiledintobytecode、notmachine-specificcode.2)

thejvmisanabstractcomputingMachineCrucialForrunningJavaProgramsDuetoitsPlatForm-IndopentInterChitecture.Itincludes:1)ClassLoaderForloadingClasses、2)Runtimedataareaforforforatastorage、3)executionEngineWithinterter、Jitcompiler、およびGarbagecolfecolfecolfececolfecolfer

jvmhasacloserelationshiptheosasittrantesjavabytecodecodecodecodecodecodecodecodecodecodecodecodecodetructions、manageSmemory、およびhandlesgarbagecollection.thisrelationshipallowsjavatorunonvariousosenvirnments、Butalsedentsはspeedifediferentjvmbeviorhiorsandosendisfredediferentjvmbehbehioorysando

Javaの実装「Write and、Run Everywherewhere」はBytecodeにコンパイルされ、Java仮想マシン(JVM)で実行されます。 1)Javaコードを書き、それをByteCodeにコンパイルします。 2)JVMがインストールされたプラットフォームでByteCodeが実行されます。 3)Javaネイティブインターフェイス(JNI)を使用して、プラットフォーム固有の機能を処理します。 JVMの一貫性やプラットフォーム固有のライブラリの使用などの課題にもかかわらず、Woraは開発効率と展開の柔軟性を大幅に向上させます。

javaachievesplatformentenceTheTheTheJavavirtualMachine(JVM)、CodetorunondifferentoperatingSystemswithOutModification.thejvmcompilesjavacodeplatform-IndopentedbyTecodeを承認することを許可します

javaispowerfulfulduetoitsplatformindepentence、object-orientednature、richstandardlibrary、performancecapability、andstrongsecurityfeatures.1)platformendependenceallowseplicationStorunonaydevicesupportingjava.2)オブジェクト指向のプログラマン型

上位のJava関数には、次のものが含まれます。1)オブジェクト指向プログラミング、サポートポリ型、コードの柔軟性と保守性の向上。 2)例外処理メカニズム、トライキャッチ式ブロックによるコードの堅牢性の向上。 3)ゴミ収集、メモリ管理の簡素化。 4)ジェネリック、タイプの安全性の向上。 5)コードをより簡潔で表現力豊かにするためのAMBDAの表現と機能的なプログラミング。 6)最適化されたデータ構造とアルゴリズムを提供するリッチ標準ライブラリ。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

メモ帳++7.3.1
使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
