廣播及廣播提示音
阿新 • • 發佈:2018-12-10
//傳送廣播
/*Button btnEnable; *//** Called when the activity is first created. *//* @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); *//* create a button. When you click the button, the alarm clock is enabled *//* btnEnable=(Button)findViewById(R.id.btnEnable); btnEnable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setReminder(true); } }); } *//** * Set the alarm * * @param b whether enable the Alarm clock or not *//* private void setReminder(boolean b) { // get the AlarmManager instance AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE); // create a PendingIntent that will perform a broadcast PendingIntent pi= PendingIntent.getBroadcast(ReminderSetting.this, 0, new Intent(this,MyReceiver.class), 0); if(b){ // just use current time as the Alarm time. Calendar c=Calendar.getInstance(); // schedule an alarm am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); } else{ // cancel current alarm am.cancel(pi); } }
}*/ //接收廣播
/* package com.Reminder; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;
*//** * Receive the broadcast and start the activity that will show the alarm *//* public class MyReceiver extends BroadcastReceiver { *//** * called when the BroadcastReceiver is receiving an Intent broadcast. *//* @Override public void onReceive(Context context, Intent intent) { *//* start another activity - MyAlarm to display the alarm *//* intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(context, MyAlarm.class); context.startActivity(intent); } }*/
/* 注意:建立完BroadcastReceiver後,需要在AndroidManifest.xml中註冊:
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name= "com.Reminder.MyReceiver" /> </intent-filter> </receiver> 2.3 提醒功能 新建一個Activity,我們在這個Activity中通過Android的Notification物件來提醒使用者。我們將新增提示音,一個TextView來顯示提示內容和並一個button來取消提醒。 其中,建立Notification主要包括: 1)獲得系統級得服務NotificationManager,通過 Context.getSystemService(NOTIFICATION_SERVICE)獲得。 2)例項化Notification物件,並設定各種我們需要的屬性,比如:設定聲音。 3)呼叫NotificationManager的notify()方法顯示Notification 詳細程式碼如下:MyAlarm.java package com.Reminder; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Audio; import android.view.View; import android.widget.Button; import android.widget.TextView; *//** * Display the alarm information *//* public class MyAlarm extends Activity { *//** * An identifier for this notification unique within your application *//* public static final int NOTIFICATION_ID=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_alarm); // create the instance of NotificationManager final NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); // create the instance of Notification Notification n=new Notification(); *//* set the sound of the alarm. There are two way of setting the sound *//* // n.sound=Uri.parse("file:///sdcard/alarm.mp3"); n.sound= Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "20"); // Post a notification to be shown in the status bar nm.notify(NOTIFICATION_ID, n); *//* display some information *//* TextView tv=(TextView)findViewById(R.id.tvNotification); tv.setText("Hello, it's time to bla bla..."); *//* the button by which you can cancel the alarm *//* Button btnCancel=(Button)findViewById(R.id.btnCancel); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { nm.cancel(NOTIFICATION_ID); finish(); } }); } }*/
//意圖傳送廣播例子 Intent intent = new Intent(); intent.setAction(“mail139_contacts_import_finish_other_process”); intent.addCategory(“mail139_import_other_process”); intent.putParcelableArrayListExtra(“infos”,ContactsEngine.getCacheAllContacts()); M9.app.sendBroadcast(intent);