2013-05-14 49 views
14

Hiện tại, tôi đang làm việc trên ứng dụng hoạt động như "Để thực hiện danh sách tác vụ". Tôi đã triển khai thành công NotificationService và SchedularService trong ứng dụng của mình. Ngoài ra tôi nhận được các cảnh báo (Thông báo) tại thời điểm thiết lập cho các nhiệm vụ. Dưới đây là các truy vấn của tôi như sau:Cách tạo báo thức liên tục ngay cả sau khi khởi động lại

  1. Với mã này, báo thức của tôi sẽ bị xóa sau khi khởi động lại? Nếu có, cách khắc phục điều này.
  2. Tôi đã giữ tính năng Ưu tiên cho các tác vụ. Nhưng tôi muốn cơ chế như vậy mà nếu người dùng chọn ưu tiên "Cao" thì anh ta sẽ nhận được thông báo ba lần, nói rằng, trước 30 phút, trước 15 phút và vào thời gian đã đặt. Làm thế nào để đạt được điều này?
  3. Tôi muốn đặt tính năng rung của Điện thoại khi Thông báo được nâng lên. Làm thế nào để đạt được điều này?
  4. Và tôi muốn biết, những gì có thể được thực hiện cho các phương pháp và hàm tạo không được chấp nhận trong NotifyService.java. Đề xuất không được chấp nhận ở cấp API 11: Notification notification = new Notification(icon, text, time);notification.setLatestEventInfo(this, title, text, contentIntent);. Trên developer.android.com, họ đã đề xuất sử dụng Notification.Builder thay thế. Vì vậy, cách làm cho ứng dụng của tôi tương thích với tất cả các cấp API.

Dưới đây là đoạn mã của tôi cho báo lịch:

... 
scheduleClient.setAlarmForNotification(c, tmp_task_id); 
... 

Dưới đây là các ScheduleClient.java lớp:

public class ScheduleClient { 

    private ScheduleService mBoundService; 
    private Context mContext; 
    private boolean mIsBound; 

    public ScheduleClient(Context context) 
    { 
     mContext = context; 
    } 

    public void doBindService() 
    { 
     mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE); 
     mIsBound = true; 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 

      mBoundService = ((ScheduleService.ServiceBinder) service).getService(); 
     } 

     public void onServiceDisconnected(ComponentName className) { 

      mBoundService = null; 
     } 
    }; 

    public void setAlarmForNotification(Calendar c, int tmp_task_id){ 

     mBoundService.setAlarm(c, tmp_task_id); 
    } 

    public void doUnbindService() { 
     if (mIsBound) 
     {   
      mContext.unbindService(mConnection); 
      mIsBound = false; 
     } 
    } 
} 

Dưới đây là các ScheduleService.java:

public class ScheduleService extends Service { 

    int task_id; 

    public class ServiceBinder extends Binder { 

     ScheduleService getService() { 

      return ScheduleService.this; 
     } 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return mBinder; 
    } 

    private final IBinder mBinder = new ServiceBinder(); 

    public void setAlarm(Calendar c, int tmp_task_id) { 

     new AlarmTask(this, c, tmp_task_id).run(); 
    } 
} 

Đây là AlarmTask.java:

public class AlarmTask implements Runnable{ 

    private final Calendar date; 
    private final AlarmManager am; 
    private final Context context; 
    int task_id; 

    public AlarmTask(Context context, Calendar date, int tmp_task_id) { 
     this.context = context; 
     this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     this.date = date; 

     task_id = tmp_task_id; 
    } 

    @Override 
    public void run() { 

     Intent intent = new Intent(context, NotifyService.class); 
     intent.putExtra(NotifyService.INTENT_NOTIFY, true); 
     intent.putExtra("task_id", task_id); 
     PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

     am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent); 
    } 
} 

Đây là NotifyService.java:

public class NotifyService extends Service { 

    public class ServiceBinder extends Binder 
    { 
     NotifyService getService() 
     { 
      return NotifyService.this; 
     } 
    } 

    int task_id; 
    private static final int NOTIFICATION = 123; 
    public static final String INTENT_NOTIFY = "com.todotaskmanager.service.INTENT_NOTIFY"; 
    private NotificationManager mNM; 
    SQLiteDatabase database; 

    @Override 
    public void onCreate() { 

     mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     String tmp_task_brief = null; 
     task_id = intent.getIntExtra("task_id", 0); 

     loadDatabase(); 
     Cursor cursor = database.query("task_info", new String[]{"task_brief"}, "task_id=?", new String[]{task_id+""}, null, null, null); 
     while(cursor.moveToNext()) 
     { 
      tmp_task_brief = cursor.getString(0); 
     } 
     cursor.close(); 

     if(intent.getBooleanExtra(INTENT_NOTIFY, false)) 
      showNotification(tmp_task_brief); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return mBinder; 
    } 

    private final IBinder mBinder = new ServiceBinder(); 

    private void showNotification(String tmp_task_brief) { 

     CharSequence title = "To Do Task Notification!!"; 
     int icon = R.drawable.e7ca62cff1c58b6709941e51825e738f; 
     CharSequence text = tmp_task_brief;  
     long time = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, text, time); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0); 

     notification.setLatestEventInfo(this, title, text, contentIntent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     mNM.notify(NOTIFICATION, notification); 

     stopSelf(); 
    } 

    void loadDatabase() 
    { 
     database = openOrCreateDatabase("ToDoDatabase.db", 
       SQLiteDatabase.OPEN_READWRITE, null); 
    } 
} 

Trả lời

32

Với mã này sẽ báo động của tôi sẽ bị xóa sau khi khởi động lại? Nếu có, cách khắc phục điều này.

Có báo động sẽ được xóa, để khắc phục điều này, bạn cần phải sử dụng phần Android gọi BroadcastReceiver như sau,

Trước tiên, bạn cần sự cho phép trong biểu hiện của bạn:

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

Ngoài ra, trong tệp kê khai của bạn, xác định dịch vụ của bạn và lắng nghe hành động đã hoàn tất khởi động:

<receiver 
    android:name=".receiver.StartMyServiceAtBootReceiver" 
    android:enabled="true" 
    android:exported="true" 
    android:label="StartMyServiceAtBootReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

Sau đó, bạn cần phải efine người nhận sẽ nhận được hành động BOOT_COMPLETED và bắt đầu dịch vụ của bạn.

public class StartMyServiceAtBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent serviceIntent = new Intent("com.myapp.NotifyService"); 
      context.startService(serviceIntent); 
     } 
    } 
} 

Và bây giờ dịch vụ của bạn sẽ chạy khi điện thoại khởi động.

2 Đối Rung

Một lần nữa bạn cần phải xác định một sự cho phép trong file AndroidManifest.xml như sau,

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

Đây là mã cho rung,

// Get instance of Vibrator from current Context 
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

// Vibrate for 300 milliseconds 
v.vibrate(300); 
+0

Vì vậy, câu trả lời cho câu hỏi của bạn, ... với mã này, bằng cách thực hiện, báo thức của tôi sẽ không bị xóa, phải không? Và tôi có nên tạo tệp java riêng biệt cho điều này hoặc thêm lớp này làm lớp con của someother không? –

+0

Có, Báo thức sẽ được duy trì vì nó sẽ được gọi trên Khởi động thiết bị mỗi lần. Bạn có thể tạo trong cùng một gói, không cần tạo gói khác. – Lucifer

+0

Xin lỗi nhưng, bạn có thể chỉ ra nơi để thực hiện mã của bạn như tôi mới để 'BroadCastReceiver' –

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