2015-12-29 12 views

Trả lời

18

trước tiên, bạn cần sử dụng trình phát quảng cáo. và bởi vì người nhận phát sóng chỉ trong một thời gian ngắn

từ blog nhà phát triển Android. Khi xử lý quảng cáo, ứng dụng được đặt thời gian cố định (hiện tại 10 giây) để thực hiện công việc. Nếu nó không hoàn thành trong thời gian đó, các ứng dụng được coi là misbehaving, và quá trình của nó ngay lập tức ném vào trạng thái nền để bị giết cho bộ nhớ nếu cần thiết.

thực tiễn tốt hơn để sử dụng dịch vụ ý định ở đây bạn có ví dụ cách thực hiện.

đây là lớp người nhận phát sóng.

public class MyReceiver extends BroadcastReceiver { 
    public MyReceiver() { 
    } 

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

     Intent intent1 = new Intent(context, MyNewIntentService.class); 
     context.startService(intent1); 
    } 
} 

và đăng ký nó trong tệp kê khai.

<receiver 
    android:name=".MyReceiver" 
    android:enabled="true" 
    android:exported="false" > 
</receiver> 

đây là lớp dịch vụ có mục đích.

public class MyNewIntentService extends IntentService { 
    private static final int NOTIFICATION_ID = 3; 

    public MyNewIntentService() { 
     super("MyNewIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Notification.Builder builder = new Notification.Builder(this); 
      builder.setContentTitle("My Title"); 
      builder.setContentText("This is the Body"); 
      builder.setSmallIcon(R.drawable.whatever); 
     Intent notifyIntent = new Intent(this, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     //to be able to launch your activity from the notification 
     builder.setContentIntent(pendingIntent); 
     Notification notificationCompat = builder.build(); 
     NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this); 
     managerCompat.notify(NOTIFICATION_ID, notificationCompat); 
    } 
} 

và đăng ký nó trong tệp kê khai.

<service 
    android:name=".MyNewIntentService" 
    android:exported="false" > 
</service> 

và sau đó trong hoạt động của bạn thiết lập các máng cỏ báo động để bắt đầu thu phát sóng tại một thời điểm cụ thể và sử dụng phương pháp AlarmManager setRepeating để lặp lại nó ví dụ này Bellow sẽ lặp lại nó mỗi ngày.

Intent notifyIntent = new Intent(this,MyReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast 
      (context, NOTIFICATION_REMINDER_NIGHT, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 
      1000 * 60 * 60 * 24, pendingIntent); 

tôi hy vọng điều này sẽ giúp bạn.

+0

Không làm việc cho tôi, đã thử với thời gian đặt thủ công nhưng vẫn không hoạt động. –

+0

Bất kỳ lý do nào để sử dụng chương trình phát sóng? Không thấy điều này là cần thiết/hữu ích. Bạn có thể trực tiếp nói chuyện với IntentService của bạn với một PendingIntent. – Zordid

6

Bạn có thể sử dụng AlarmManager để thiết lập báo động ở thời gian quy định

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
if (!prefs.getBoolean("firstTime", false)) { 

    Intent alarmIntent = new Intent(this, AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); 

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 7); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 1); 

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

    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putBoolean("firstTime", true); 
    editor.apply(); 
} 

tôi đã sử dụng SharedPreferences để kiểm tra đó không phải là lần đầu tiên để chạy các ứng dụng và nếu nó là, bạn thiết lập báo động mà nếu không làm gì thay vì đặt báo thức mỗi lần bạn khởi động ứng dụng của mình.
Sử dụng một BroadcastReceiver lắng nghe khi báo động xảy ra

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // show toast 
     Toast.makeText(context, "Alarm running", Toast.LENGTH_SHORT).show(); 
    } 
} 

Sử dụng máy thu khác để nghe khởi động thiết bị để bạn có thể thiết lập lại báo động

public class DeviceBootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      // on device boot compelete, reset the alarm 
      Intent alarmIntent = new Intent(context, AlarmReceiver.class); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 

      AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      calendar.set(Calendar.HOUR_OF_DAY, 7); 
      calendar.set(Calendar.MINUTE, 0); 
      calendar.set(Calendar.SECOND, 1); 

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

thêm phép manifest

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

sau đó đăng ký người nhận của bạn

<receiver android:name=".DeviceBootReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 
<receiver android:name=".AlarmReceiver" /> 
+0

Điều gì xảy ra nếu nó cũng gửi thông báo khi mở ứng dụng? – Si8

+0

Bạn có thể thêm mã thông báo trong phương thức 'onCreate()' của hoạt động trình khởi chạy. – SaNtoRiaN

+0

Cảm ơn bạn đã trả lời. 'SharedPReference' hoạt động như thế nào vào ngày hôm sau? Bởi vì boolean sẽ là false – Si8

Các vấn đề liên quan