一个Demo学完Android中所有的服务(3)

使用sendBroadcast,向一个Action发送广播,并由相应的广播接收器接收并执行相应的动作

实现的代码如下:

4.1 打开广播服务

package lovefang.stadyService;
/**引入包*/
 import Android.view.View;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.widget.Button;
/**使用Broadcast,这是一个发送广播的类*/
 public class UseBroadcast extends Activity{
   /**创建参数*/
  private Button sendBroadcast;
   /**创建Activity*/
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.broadcast);// 使用布局文件
   getView();
   sendBroadcast.setOnClickListener(sendBroadcastClick);// 添加事件监听
  }
  public void getView(){
   sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
  }
   /**创建事件监听*/
  public Button.OnClickListener sendBroadcastClick = new Button.OnClickListener(){
   public void onClick(View view){
    Intent intent = new Intent();// 创建意图
    intent.putExtra("CONTENT",  "This is a Braodcast demo");// 设置广播的内容
    intent.setAction("lovefang.stadyService");// 设置广播的Action
    sendBroadcast(intent);
   }
  };
 
 }

4.2 处理广播消息

package lovefang.stadyService;
/***/
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.util.Log;
/**这是一个接收广播的类*/
 public class UseBroadcastReceiver extends BroadcastReceiver{
  public void onReceive(Context context, Intent intent){
   Log.v("UseBroadcastReceiver", "I get a message");
  }
 }

5.Notification

这个称之为通知,显示在手机的通知栏,用户可以清除,可以点击

实现的代码如下:

package lovefang.stadyService;

import android.content.Intent;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.net.Uri;
 import android.media.RingtoneManager;
 import android.widget.Button;
 import android.view.View;

/**使用notification*/
 public class UseNotification extends Activity {
   /**创建组件*/
  private Button textButton;
  private Button soundButton;// 声音通知
  private Button vibrateButton;// 震动通知
  private Button ledButton;// led通知
  private Button offButton;// 关闭通知
  NotificationManager notificationManager;
   /**创建Activity*/
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.notification);
   getComment();
   registerComment();
  }
   /**获取对象*/
  public void getComment(){
    /**获取Notification对象*/
   notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
   textButton = (Button)findViewById(R.id.notificationMessage);
   soundButton =(Button)findViewById(R.id.notificationSound);
   vibrateButton = (Button)findViewById(R.id.notificationVibrate);
   ledButton = (Button)findViewById(R.id.notificationLED);
   offButton = (Button)findViewById(R.id.offnotification);
  }
   /**注册对象*/
  public void registerComment(){
   textButton.setOnClickListener(notificationMessage);
   soundButton.setOnClickListener(notificationSound);
   vibrateButton.setOnClickListener(notificationVibrate);
   ledButton.setOnClickListener(notificationLed);
   offButton.setOnClickListener(notificationOff);
  }
  public Button.OnClickListener notificationMessage = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 创建Notification对象
    notification.icon = R.drawable.icon;
    notification.tickerText = "This is text notication";// 设置通知消息
     /**单击通知后的Intent,此例子单击后还是在当前页面*/
    PendingIntent intent = PendingIntent
     .getActivity(UseNotification.this,
       0, new Intent(UseNotification.this,UseNotification.class)
       , 0);
     /**设置通知消息*/
    notification.setLatestEventInfo(UseNotification.this
      ,"Notification","Content of Notification Demo",intent);
     /**执行通知*/
    notificationManager.notify(0, notification);
   }
  };
  public Button.OnClickListener notificationSound = new Button.OnClickListener(){
   public void onClick(View view){
     /**创建通知对象*/
    Notification notification = new Notification();
     /**获取系统当前声音*/
    String ringName = RingtoneManager.getActualDefaultRingtoneUri(
      UseNotification.this, RingtoneManager.TYPE_RINGTONE)
      .toString();
     /**设置系统当前铃声为此通知的铃声*/
    notification.sound = Uri.parse(ringName);
     /**执行通知*/
    notificationManager.notify(0,notification);
   }
  };
   /**震动通知*/
  public Button.OnClickListener notificationVibrate = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 创建Notification对象
    notification.vibrate = new long[] {0, 100, 200, 300};// 设置通知震动模式
    notificationManager.notify(0,notification);// 执行通知
   }
  };
   /**LED通知*/
  public Button.OnClickListener notificationLed = new Button.OnClickListener(){
   public void onClick(View view){
    Notification notification = new Notification();// 创建Notification对象
    notification.ledOnMS = 300;// 设置led开始闪光的时间
    notification.ledOffMS = 1000;// 设置关闭时的闪光时间
    notificationManager.notify(0,notification);// 执行通知
   }
  };
   /**关闭通知*/
  public Button.OnClickListener notificationOff = new Button.OnClickListener(){
   public void onClick(View view){
    notificationManager.cancel(0);// 关闭通知
   }
  };
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/595c5e7b95fbfbb1ca304e8bf8d8aa42.html