2013-12-17 32 views

回答

0

首先要建立火災報警按鈕經理 -

@覆蓋

public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(v==btnStartAlarm){ 
      fireAlarm(); 
     } 

這裏是我的報警管理器 -

public void fireAlarm() { 
     /** 
     * call broadcost reciver 
     */ 
     Intent intent = new Intent(context, AlarmReceiver.class); 
     intent.setAction("com.manish.alarm.ACTION"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 111, intent,0); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     AlarmManager alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE); 
     alarm.cancel(pendingIntent); 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),100000, pendingIntent); 
    } 

現在創建一個BroadcastReceiver類和火法的通知 -

public class AlarmReceiver extends BroadcastReceiver { 
    private final String SOMEACTION = "com.manish.alarm.ACTION"; 

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

     String action = intent.getAction(); 
     if (SOMEACTION.equals(action)) { 
      //do what you want here 
      generateNotification(context,"Hi how are you?"); 
     } 
    } 

    @SuppressWarnings("deprecation") 
    private void generateNotification(Context context, String message) { 
      System.out.println(message+"++++++++++2"); 
      int icon = R.drawable.ic_launcher; 
      long when = System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(icon, message, when); 
      String title = context.getString(R.string.app_name); 
      String subTitle = context.getString(R.string.app_name); 
      Intent notificationIntent = new Intent(context, OutPut.class); 
      notificationIntent.putExtra("content", message); 
      PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      notification.setLatestEventInfo(context, title, subTitle, intent); 
      //To play the default sound with your notification: 
      notification.defaults |= Notification.DEFAULT_SOUND; 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notificationManager.notify(0, notification); 



    } 

} 

,並創建一個用於顯示通知類MESSAGE-

public class OutPut extends Activity { 
    TextView textmessage; 
    String stringValue; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_output); 

     textmessage = (TextView) findViewById(R.id.textView1); 

     Intent intent = getIntent(); 
     stringValue = intent.getStringExtra("content"); 
     textmessage.setText(stringValue); 
     System.out.println(stringValue); 

    } 
} 

和你的manifest.xml裏面只是把權限抗振

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

這裏從github- http://www.androidhub4you.com/2013/12/android-alarm-manager-example-how-to.html

https://github.com/manishsri01/AndroidAlarmManagerDemo下載鏈接

+0

所以我們需要在項目中創建3個java文件和2個xml文件。我對嗎? – user3057567

+0

是................... –

+0

我試過了不行我 – user3057567