您好我想开发一个没有Main Activity作为启动器的简单应用程序。
我想注册一个广播接收器,它在设备重启后启动,并在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>
这是我的广播接收器
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
最后是MainActivity
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);
}
}
我的代码有什么问题?
提前致谢
弗朗西斯
答案 0 :(得分:1)
我的代码正在运作......它就是......
清单中的
<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>
答案 1 :(得分:0)
我看不出你的代码有什么问题,但是我有值得尝试的东西。
将权限移出application
代码:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
如果无效,请简化receiver
:
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
仍然无法使用?尝试添加一些延迟,如上所述[{3}}:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//run your service
}
}, 10000);
引自上面的链接:
虽然,我建议延迟几秒钟,例如10秒钟 运行(1)线,这对于不同的手机而言更稳定 服务。
例如,就我而言,我的服务是写sd卡。如果你 立即开始你的服务,有些手机可能因为sd而失败 卡尚未就绪。
答案 2 :(得分:0)
从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).
您需要以某种方式启动您的应用程序,然后将其发送到休眠状态(但在应用程序管理器中注册)。您可以使用此服务。
答案 3 :(得分:0)
强烈建议不要从BroadcastReciever启动Activity: https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
永远不要启动活动以响应传入的广播意图。
在我的情况下 PackageManager.DONT_KILL_APP 帮助: https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
我已经尝试过MIUI 8固件真实设备Xiaomi Redmi Note 3.
我的发现是:
您必须将应用程序添加到自动运行才能通过广播触发它。我已经用Viber,WhatsApp这样严肃的应用程序检查了它。
我与清单设置进行了比较(没有以编程方式启用接收器):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>