[英]Android App with Broadcast receiver after reboot as main
您好,我要開發一個沒有主活動作為啟動器的簡單應用程序。
我想注冊一個廣播接收器,它在設備重啟后啟動,並且在OnReceive回調內部啟動一個Activity
這是我的清單
<?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);
}
}
我的代碼有什么問題?
提前致謝
弗朗切斯科
我的代碼正在工作..在這里...
在清單中
<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>
我看不到您的代碼有任何問題,但是我值得嘗試。
將權限移出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>
還是行不通? 嘗試添加一些延遲,提到這里 :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//run your service
}
}, 10000);
從上面的鏈接引用:
同時,我建議在運行(1)線之前要延遲幾秒鍾,例如10秒,這對於不同的電話和服務來說更穩定。
例如,在我的情況下,我的服務將要寫入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).
您需要以某種方式啟動您的應用程序,然后將其發送到休眠狀態(但已在應用程序管理器中注冊)。 您可以為此使用服務。
強烈不建議從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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.