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

重新启动后以广播接收器为主的Android应用程序

重新启动后以广播接收器为主的Android应用程序,android,broadcastreceiver,Android,Broadcastreceiver,您好,我想开发一个简单的应用程序,没有主要活动作为启动器 我想注册一个广播接收器,它在设备重新启动后启动,在OnReceive回调中启动一个活动 这是我的舱单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.examples" android:versionCode

您好,我想开发一个简单的应用程序,没有主要活动作为启动器

我想注册一个广播接收器,它在设备重新启动后启动,在OnReceive回调中启动一个活动

这是我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="it.examples"
      android:versionCode="1"
      android:versionName="1.0"
        >
    <uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="18" />



    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">


        <receiver android:name=".AfterRebootBR" android:exported="false" 
    android:label="Boot Notification Receiver" android:enabled="true"
         android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

               <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>


        </receiver>

        <activity android:name=".MainActivity"
                  android:label="@string/app_name">

        </activity>
    </application>
</manifest>
最后是主要活动

package it.examples;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
我的代码有什么问题

提前谢谢


弗朗西斯科

我的代码正在工作..这是它

明显地

   <receiver
        android:name="com.calender.calenderevent.Reboot_Reciever"
        android:enabled="true"
        android:exported="true"
        android:label="BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" >
            </action>
        </intent-filter>
    </receiver>

我看不出你的代码有什么问题,但是我有一些东西值得一试

将权限移出
应用程序
标记:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
还是不工作?尝试增加一些延迟,如前所述:

引自上述链接:

然而,我建议延迟几秒,例如,10秒,然后 运行(1)条线路,对于不同的手机和 服务

例如,在我的例子中,我的服务将写入sd卡。如果你 立即启动您的服务,有些手机可能会因为sd而出现故障 卡还没准备好


从android 3.1开始,如果应用程序服务管理器没有处于活动状态的上下文(即,至少有一个活动或服务将进程保持在“活动状态”),那么它就不能生成广播接收器

规范摘录

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. 
It does this to prevent broadcasts from background services from inadvertently or 
unnecessarily launching components of stoppped applications. A background service or 
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES 
flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet 
launched and when they are manually stopped by the user (in Manage Applications).

您需要以某种方式启动应用程序,然后以休眠状态发送(但在应用程序管理器中注册)。您可以为此使用服务。

强烈建议不要从BroadcastReceiver启动活动:

切勿启动活动以响应传入的广播意图

在我的例子中,PackageManager.DONT_KILL_应用程序帮助:

我已经试用过MIUI8固件真实设备小米Redmi Note 3

我的发现是:

  • 您必须将应用程序添加到自动运行,以使其能够通过广播启动。我已经用Viber、WhatsApp等严肃的应用程序检查过了

  • 我比较了清单设置(不以编程方式启用Receiver):

    
    


你的意思是“接收”时从未发射过“代码”吗?是的,接收从未发射过?我添加了日志行和Toast消息,但没有显示。只有当呈现具有典型意图过滤器(action=android.intent.action.MAIN和category=android.intent.category.LAUNCHER)的活动时,广播才起作用。感谢您的回答,但也尝试了您的解决方案,因为它不起作用。你能发布你的清单文件吗?对我来说,你有一个带有典型意图过滤器的活动(action=android.intent.action.MAIN和category=android.intent.category.LAUNCHER)。我按照你的建议更改了清单文件的一部分,但在重新启动后不会调用OnReceive方法
    <receiver android:name=".AfterRebootBR">
           <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //run your service
            }
        }, 10000);
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. 
It does this to prevent broadcasts from background services from inadvertently or 
unnecessarily launching components of stoppped applications. A background service or 
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES 
flag to broadcast intents that should be allowed to activate stopped applications.

Applications are in a stopped state when they are first installed but are not yet 
launched and when they are manually stopped by the user (in Manage Applications).
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();

pm.setComponentEnabledSetting(receiver,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
        PackageManager.DONT_KILL_APP);