Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.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_Broadcastreceiver_Android Service - Fatal编程技术网

尝试在Android上启动服务

尝试在Android上启动服务,android,broadcastreceiver,android-service,Android,Broadcastreceiver,Android Service,我一直试图在设备启动安卓系统时启动一项服务,但我无法让它工作。我在网上看过很多链接,但没有一个代码是有效的。我忘了什么吗 AndroidManifest.xml 我有一个额外的-标签,不知道这是否有什么区别 <receiver android:name="BootIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BO

我一直试图在设备启动安卓系统时启动一项服务,但我无法让它工作。我在网上看过很多链接,但没有一个代码是有效的。我忘了什么吗

AndroidManifest.xml
我有一个额外的
-标签,不知道这是否有什么区别

<receiver android:name="BootIntentReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
            <category android:name="android.intent.category.HOME" />  
        </intent-filter>  
</receiver>


您是否尝试过使用if子句“android.intent.action.BOOT_COMPLETED”.equals(intent.getAction(),因为接收方可能只接收到该意图?

我认为您的清单需要添加:

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

作为附加信息:在装入外部存储器之前,会将BOOT_COMPLETE发送到应用程序。因此,如果将应用程序安装到外部存储器,则不会收到BOOT_COMPLETE广播消息


有关收听“引导完成”的广播接收器部分的更多详细信息,请注意问题开头有一个打字错误:

而不是:


一个小“uu”和所有这些麻烦:)

其他答案看起来不错,但我想我应该把所有的东西都总结成一个完整的答案

AndroidManifest.xml
文件中需要以下内容:

  • 元素中:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    (您不需要
    android:enabled
    exported
    等属性:android默认值是正确的)

    MyBroadcastReceiver.java
    中:

    package com.example;
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent startServiceIntent = new Intent(context, MyService.class);
            context.startService(startServiceIntent);
        }
    }
    
  • 原问题:

    • 不清楚
      元素是否在
      元素中
    • 不清楚是否为
      BroadcastReceiver
      指定了正确的完全限定(或相对)类名

    我刚才发现可能是因为
    设置中的
    快速启动
    选项
    电源

    当我关闭此选项时,我的应用程序将接收到此广播的消息,但不会接收其他消息

    顺便说一句,我在HTC S上安装了安卓2.3.3

    希望能有所帮助。

    以及

    <action android:name="android.intent.action.BOOT_COMPLETED" />  
    
    
    
    也用

    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    
    
    

    HTC设备似乎无法捕获BOOT_COMPLETED(启动完成)

    事实上,我不久前就遇到了这个问题,而且很容易修复,如果您设置了
    “android.intent.action.BOOT_COMPLETED”(安卓.intent.action.BOOT_COMPLETED)权限和意图过滤器,您实际上不会出错

    请注意,如果你在Android 4.X上,你必须在启动服务之前运行广播侦听器,这意味着,你必须首先添加一个活动,一旦你的广播接收器运行,你的应用程序应该如你所期望的那样运行,然而,在Android 4.X上,我还没有找到一种方法在启动服务时不进行任何活动,我认为谷歌这样做是出于安全考虑。

    请参考此链接
    使用服务启动的分步过程

    如何启动设备启动服务(自动运行应用程序等)

    首先:从Android 3.1+版本开始,如果用户至少从未启动过您的应用程序一次或用户“强制关闭”应用程序,您将不会收到BOOT_COMPLETE。 这样做是为了防止恶意软件自动注册服务。这个安全漏洞在较新版本的Android中被填补

    解决方案:

    创建带有活动的应用程序。当用户运行它一次,应用程序可以收到启动完成广播消息

    第二:在装入外部存储器之前发送BOOT_COMPLETE。如果应用程序安装到外部存储器,它将不会收到BOOT_COMPLETE广播消息

    在这种情况下,有两种解决方案:

  • 将应用程序安装到内部存储
  • 在内部存储中安装另一个小应用程序。此应用程序接收启动完成并在外部存储上运行第二个应用程序
  • 如果您的应用程序已经安装在内部存储器中,那么下面的代码可以帮助您了解如何在设备启动时启动服务


    在Manifest.xml中

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    ... >
    
    许可:

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

    对于HTC,如果设备未捕获接收\启动\完成,您可能还需要添加此代码的清单:

    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    
    如何获取设备id?获取id为的已连接设备的列表:

    adb devices
    
    ADT中的adb默认情况下,您可以在以下位置找到:

    adt-installation-dir/sdk/platform-tools
    

    享受

    如果将空构造函数留在receiver类中,我将面临这个问题。在删除空contsructor onRreceive方法后,该方法开始正常工作。

    如果您使用的是Android Studio,并且非常喜欢自动完成,那么我必须通知您,我使用的是Android Studio v 1.1.0,我使用自动完成以获得以下权限

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    
    
    Android Studio自动完成了
    RECEIVE\u BOOT\u completed
    都是小写的,比如
    RECEIVE\u BOOT\u completed
    ,我一直很紧张,因为我已经勾选了启动服务时要做的事情清单。我刚刚再次确认

    Android Studio会以小写形式自动完成此权限


    正如@Damian所评论的,这个帖子中的所有答案都是错误的。像这样手动操作会使你的服务从设备进入睡眠的中间停止。您需要先获得唤醒锁。幸运的是,我们可以这样做:

    public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This is the Intent to deliver to our service.
            Intent service = new Intent(context, SimpleWakefulService.class);
    
            // Start the service, keeping the device awake while it is launching.
            Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
            startWakefulService(context, service);
        }
    }
    
    然后,在您的服务中,确保释放唤醒锁:

        @Override
        protected void onHandleIntent(Intent intent) {
            // At this point SimpleWakefulReceiver is still holding a wake lock
            // for us.  We can do whatever we need to here and then tell it that
            // it can release the wakelock.
    
    ...
            Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
            SimpleWakefulReceiver.completeWakefulIntent(intent);
        }
    
    别忘了将WAKE_LOCK Permission添加到mainfest:

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

    在尝试了所有提到的答案和技巧之后,我终于找到了为什么我的手机中的代码不起作用。一些安卓手机,如“华为荣誉3C安卓4.2.2”在其设置中有一个Statup Manager菜单,您的应用程序必须在列表中选中。
    :)

    在安装外部存储之前,启动完成将被发送执行。如果您的应用安装到外部存储,它将不会收到启动完成广播消息。为了防止这种情况,您可以在内部存储中安装应用程序。只需在menifest.xml中添加这一行就可以做到这一点

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    ... >
    
    
    
    一些
    adb devices
    
    adt-installation-dir/sdk/platform-tools
    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This is the Intent to deliver to our service.
            Intent service = new Intent(context, SimpleWakefulService.class);
    
            // Start the service, keeping the device awake while it is launching.
            Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
            startWakefulService(context, service);
        }
    }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            // At this point SimpleWakefulReceiver is still holding a wake lock
            // for us.  We can do whatever we need to here and then tell it that
            // it can release the wakelock.
    
    ...
            Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
            SimpleWakefulReceiver.completeWakefulIntent(intent);
        }
    
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    ... >
    
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                </intent-filter>
    
    public class BootReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //whatever you want to do on boot
           Intent serviceIntent = new Intent(context, YourService.class);
           context.startService(serviceIntent);
        }
    }
    
    <manifest...>
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <application...>
            <receiver android:name=".BootReceiver" android:enabled="true" android:exported="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
        ...
    
    ...
     final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootReceiver.class.getName());
            if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
            getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
    ...