1

我試圖在固定時間向用戶顯示通知。我正在使用BroadcastReceiver來顯示通知。問題在於,BroadcastReceiver僅在應用程序在前臺或後臺運行時調用。但是當我關閉應用程序(意味着關閉應用程序)後不會調用BroadcastReceiver。我正在使用AlarmManager致電BroadcastReceiver關閉我的應用程序後未調用BroadcastReceiver

我在某個地方讀到了stackoverflow,我們沒有專門打電話給BroadcastReceiver。我們只需要在AndroidManifest中註冊它。

我使用的是秀美紅米3s主裝置和android版本是6.0.1。

那麼我錯過了什麼?爲什麼BroadcastReceiver不叫?

這是我在哪裏觸發AlarmManager

public class Settings extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.settings,container,false); 


     Intent intent = new Intent(getActivity(), BroadcastReciever.class); 

     AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Service.ALARM_SERVICE); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 15); 
     calendar.set(Calendar.MINUTE, 27); 
     calendar.set(Calendar.SECOND, 00); 

     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent); 

     return view; 
    } 
} 

這是我的課我BroadcastReceiver

public class BroadcastReciever extends BroadcastReceiver { 

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    @Override 
    public void onReceive(Context context, Intent intent) { 


     Intent nofificationIntent = new Intent(context, Home.class); 
     nofificationIntent.putExtra("notify_key","notify_value"); 

     PendingIntent resultPendingIntent = 
       PendingIntent.getActivity(
         context,1, 
         nofificationIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.donate) 
         .setSound(alarmSound) 
         .setContentIntent(resultPendingIntent) 
         .setContentTitle("Whats up") 
         .setContentText("Hello world"); 

     int mNotificationId = 001; 

     NotificationManager mNotifyMgr = 
       (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

    } 
} 

這是我的清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.user.nf"> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_CALENDAR"/> 
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".Splash"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

     <activity android:name=".Home" 
      android:windowSoftInputMode="stateHidden" 
      android:screenOrientation="portrait"/> 

     <activity android:name=".AndroidDatabaseManager" 
      android:theme="@style/Theme.AppCompat.Light"/> 

     <receiver android:name="broadcast.BroadcastReciever" 
      android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.NOTIFY" /> 
     </intent-filter> 
     </receiver> 

    </application> 

</manifest> 
+0

我在其他手機上測試了我的應用程序,並在其他手機上正常工作。 mi設備中可能存在一些內部定製miui os,它正在停止廣播以進行呼叫。 –

回答

2

使用WakefulBroadcastReceiver代替

幫助器實現BroadcastReceiver的常見模式,該模式接收設備喚醒事件,然後將工作傳遞給Service,同時確保設備在轉換期間不會回到休眠狀態。

此課程負責爲您創建和管理部分喚醒鎖;您必須請求WAKE_LOCK權限才能使用它。

documentation這裏

希望它可以幫助!

0

在活動: -

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    startService(new Intent(MainActivity.this,MyFirstService.class)); 
} 

在服務: -

公共類MyFirstService延伸服務 {

@Nullable 
@Override 
public IBinder onBind(Intent intent) 
{ 
    return null; 
} 

@Override 
public void onCreate() 
{ 
    super.onCreate(); 
    Notification note = new Notification(0,null,System.currentTimeMillis()); 
    note.flags |= Notification.FLAG_NO_CLEAR; 
    startForeground(42, note); 
    stopService(new Intent(this, MyService.class)); 
    stopForeground(true); 
} 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) 
{ 
    return START_STICKY; 
} 

}

在廣播: -

在廣播中創建serivce後編寫代碼。

相關問題