Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何创建在启动序列完成后启动服务的应用程序?_Android_Android Broadcastreceiver - Fatal编程技术网

Android 如何创建在启动序列完成后启动服务的应用程序?

Android 如何创建在启动序列完成后启动服务的应用程序?,android,android-broadcastreceiver,Android,Android Broadcastreceiver,我想创建一个在重启后启动服务的应用程序,但我不想显示UI——就像服务在后台无声运行一样。我可以创建它,但重新启动后应用程序崩溃。因为MainActivity尚未启动,我不想启动任何活动。我如何解决这个问题 My manifest.xml: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.galleryapp"> <uses-permission android:

我想创建一个在重启后启动服务的应用程序,但我不想显示UI——就像服务在后台无声运行一样。我可以创建它,但重新启动后应用程序崩溃。因为MainActivity尚未启动,我不想启动任何活动。我如何解决这个问题

My manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.galleryapp">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:name=".App"
    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">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".CameraEventReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="com.android.camera.NEW_PICTURE" />
        </intent-filter>
    </receiver>

     <receiver android:name="com.galleryapp.RebootDeviceReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
     </receiver>

    <service
        android:name=".BackgroundService"
        android:exported="true" />

</application>

您需要编写一个BroadcastReceiver并将其注册到清单文件中

<receiver android:name="your.package.BootCompletedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

如果您想让服务运行更长的时间,您需要将其作为前台服务运行,至少对于较新的Android版本是如此。

您需要编写一个BroadcastReceiver并将其注册到清单文件中

<receiver android:name="your.package.BootCompletedReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

如果您想让服务运行更长的时间,您需要将其作为前台服务运行,至少对于较新的Android版本是这样。

我已经定义了它。您可以在问题中看到“…我可以创建它,但重新启动后应用程序崩溃…”。问题是,我如何在没有任何活动/UI的情况下运行后台服务。你能在没有任何UI的情况下使用此代码运行服务吗?是的,服务从来没有UI——除了前面提到的前台服务,前台服务需要显示通知,以便用户知道有东西在运行(并耗尽电池或监视他等等)我知道在这之前我试过前台服务。但是,当重新启动完成时,应用程序崩溃。因为有一个主活动,但我不想运行主活动。如果mainactivity尚未运行,则意图将为null。哪个意图为null?将意图传递给onReceive?您可以检查:
如果(!Intent.ACTION_BOOT_COMPLETED.equals(Intent.getAction()){return;}
我已经定义了它。您可以在问题中看到“…我可以创建它,但重新启动后应用程序崩溃…”。问题是,我如何在没有任何活动/UI的情况下运行后台服务。你能在没有任何UI的情况下使用此代码运行服务吗?是的,服务从来没有UI——除了前面提到的前台服务,前台服务需要显示通知,以便用户知道有东西在运行(并耗尽电池或监视他等等)我知道在这之前我试过前台服务。但是,当重新启动完成时,应用程序崩溃。因为有一个主活动,但我不想运行主活动。如果mainactivity尚未运行,则意图将为null。哪个意图为null?将意图传递给onReceive?您可以检查:
如果(!Intent.ACTION_BOOT_COMPLETED.equals(Intent.getAction()){return;}
发布崩溃日志。我不知道如何获取崩溃日志。因为问题发生在重新启动之后,我无法在重新启动期间运行应用程序。但是我在Ridcully的回答下解释说你可以看到问题。发布崩溃日志。我不知道如何获取崩溃日志。因为问题发生在重新启动之后,我无法在重新启动期间运行应用程序。但我解释说,在Ridcully的回答下,你可以看到问题所在。
public class BootCompletedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent msgIntent = new Intent(context, MyJobIntentService.class);
        msgIntent.setAction(MyJobIntentService.ACTION_RESCHEDULE);
        MyJobIntentService.enqueueWork(context, msgIntent);
    }
}