2012-02-09 35 views
7

我試圖開發一個示例報警應用程序。我搜查了Google和SC,他們大部分的例子都很困惑。如何創建符合下列要求的鬧鐘應用,如何開發Android報警應用程序

  1. 在我的主頁屏幕上我有一個按鈕,像「啓動報警」,當我點擊該按鈕時間選擇器必須啓用。

  2. 我按照自己的意願選擇時間,一旦選擇時間,小部件上將啓用鬧鐘圖標。 (例如,如果我們將鬧鐘設置爲默認移動鬧鐘應用程序,圖標將被啓用,表示鬧鐘已設置)。

  3. 當達到設定的時間(從TimePicker應用程序設置的時間)時,鬧鐘將發出蜂鳴聲。

這些都是我的要求,我完成了前兩點,但我仍然在設置報警方面掙扎。

+0

你,但我在努力從過去的2天 – Aerrow 2012-02-09 10:33:52

+0

哥們,只要創建一個應用程序。首先,瞭解應用程序創建的一些小部件。並且,把時間表放在那裏。並且,將數據庫中的時間(以毫秒爲單位)存儲起來,以便提醒您。那時,只需從數據庫中調用該時間並將其檢查到當前時間(以毫秒爲單位)。在所需的時間內,只需從警報管理器或通知管理器的通知中引發警報。 – Praveenkumar 2012-02-09 10:44:22

+0

你有這方面的任何樣品,因爲我沒有想法如何做到這一點。 – Aerrow 2012-02-09 11:31:23

回答

7

看看AlarmManager。而且,如果您想同時使用警報,則必須使用Service類。並且,請參閱下面的示例代碼 -

public class OnBootReceiver extends BroadcastReceiver { 
    private static final int PERIOD=300000; // 5 minutes 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    AlarmManager mgr = 
     (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(context, OnAlarmReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); 
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
     SystemClock.elapsedRealtime()+60000, PERIOD, pi); 
    } 

這將每6分鐘重複一次警報。請參閱Scheduling Repeating Alarms文檔。

+0

我下載了鏈接的代碼,因爲你給,一旦我跑該代碼什麼都不會發生,是否有任何程序來運行此代碼,請告訴我 – Aerrow 2012-02-09 10:25:36

+0

請參閱,輸出將被放置在您的日誌貓和您的DDMS只是在代碼中看到。你只需要根據你的需要修改它。 – Praveenkumar 2012-02-09 10:27:25

+0

@Aerrow添加到您的maniftest文件。 – computerquest 2017-07-25 13:07:57

0

要完成你的最後一點,你需要做Date Comparision和使用AlaramManager Alaram Doc,再次需要使用Service 來比較下一個日期和時間。希望它對你有幫助。

+0

我試過一些示例代碼,但它並沒有太大的成效,作品罰款但沒有警報聲和跡象 – Aerrow 2012-02-09 10:04:11

+0

行。然後在那裏使用通知。或使用鈴聲管理器或使用uri訪問音調,看到這可能會幫助你http://developer.android.com/reference/android/media/RingtoneManager.html – OnkarDhane 2012-02-09 10:11:56

0

您需要使用RingtoneManagerNotificationManager(顯示在屏幕頂部的任何文本或圖像以供用戶通知),或者你可以使用MediaPlayer設置到達報警時間時播放的聲音。您必須在清單文件中設置<receiver>標記,該標記必須包含延伸爲BroadCastReceiver的類。在接收器類中,您可以編寫代碼來喚醒設備。

3

當您啓用警報時,您必須調用內置警報管理器並使用alarmmanager.set設置管理器中的警報時間。 一旦報警時間(毫秒)被給予警告管理員將通過reciever類

//creating and assigning value to alarm manager class 
    Intent AlarmIntent = new Intent(MainActivity.this, AlarmReciever.class); 
    AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE); 
    PendingIntent Sender = PendingIntent.getBroadcast(MainActivity.this, 0, AlarmIntent, 0);  
    AlmMgr.set(AlarmManager.RTC_WAKEUP, Alarm.getTimeInMillis(), Sender); 

發送消息,您可以retrive消息對於recieving報警你必須做出延伸reciever一個新類在onrecieve你可以設置意圖的活動你想打電話報警時間,你也可以提供通知。

public class AlarmReciever extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) 
{ //Build pending intent from calling information to display Notification 
    PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
    NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); 
    Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis()); 
    noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", Sender); 
    noti.flags = Notification.FLAG_AUTO_CANCEL; 
    manager.notify(R.string.app_name, noti); 

    //intent to call the activity which shows on ringing 
    Intent myIntent = new Intent(context, Alarmring.class); 
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(myIntent); 

    //display that alarm is ringing 
    Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show(); 
}} 

如果你仍然得到的任何問題再問一遍.. :)