标签:
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图
接下来,以源代码的方式分析这个例子
1.MainActivity--主界面
这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:
- package lovefang.stadyService;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.content.Intent;
- import android.util.Log;
- public class MainStadyServics extends Activity {
-
- Button startServiceButton;
- Button shutDownServiceButton;
- Button startBindServiceButton;
- Button sendBroadcast;
- Button notificationButton;
- Button alarmButton;
- Button handlerButton;
- Button asyncButton;
- Button phoneStateButton;
- Button callphoneButton;
- Button vibratorButton;
- CountService countService;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.v("MainStadyServics", "setContentView");
- setContentView(R.layout.main);
- getWidget();
- regiestListener();
- }
-
- public void getWidget(){
- startServiceButton = (Button)findViewById(R.id.startServerButton);
- startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);
- shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);
- sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
- notificationButton = (Button)findViewById(R.id.notification);
- alarmButton = (Button)findViewById(R.id.alarm);
- handlerButton = (Button)findViewById(R.id.handler);
- asyncButton = (Button)findViewById(R.id.async);
- phoneStateButton = (Button) findViewById(R.id.phonestate);
- callphoneButton = (Button) findViewById(R.id.callphone);
- vibratorButton = (Button) findViewById(R.id.vibrator);
- }
-
- public void regiestListener(){
- startServiceButton.setOnClickListener(startService);
- shutDownServiceButton.setOnClickListener(shutdownService);
- startBindServiceButton.setOnClickListener(startBinderService);
- sendBroadcast.setOnClickListener(broadcastReceiver);
- notificationButton.setOnClickListener(notification);
- alarmButton.setOnClickListener(startAlarm);
- handlerButton.setOnClickListener(handler);
- asyncButton.setOnClickListener(async);
- phoneStateButton.setOnClickListener(phonestate);
- callphoneButton.setOnClickListener(callphoneEvent);
- vibratorButton.setOnClickListener(vibrator);
- }
-
- public Button.OnClickListener startService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
- startService(intent);
- Log.v("MainStadyServics", "start Service");
- }
- };
-
- public Button.OnClickListener shutdownService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
-
- stopService(intent);
- Log.v("MainStadyServics", "shutDown serveice");
- }
- };
-
- public Button.OnClickListener startBinderService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,UseBrider.class);
- startActivity(intent);
- Log.v("MainStadyServics", "start Binder Service");
- }
- };
-
- public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);
- startActivity(intent);
- Log.v("MainStadyServics","start broadcast");
- }
- };
-
- public Button.OnClickListener notification = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseNotification.class);
- startActivity(intent);
- Log.v("MainStadyService ","start Notification");
-
- }
- };
-
- public Button.OnClickListener startAlarm = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);
- startActivity(intent);
- Log.v("MainStadyService ","start alarm");
-
- }
- };
- public Button.OnClickListener handler= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);
- startActivity(intent);
- Log.v("MainStadyService ","start handle");
- }
- };
- public Button.OnClickListener async= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);
- startActivity(intent);
- Log.v("MainStadyService ","start handle");
- }
- };
- public Button.OnClickListener phonestate= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);
- startActivity(intent);
- Log.v("MainStadyService ","start phonestate");
- }
- };
- public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);
- startActivity(intent);
- Log.v("MainStadyService ","start callphone");
- }
- };
- public Button.OnClickListener vibrator= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);
- startActivity(intent);
- Log.v("MainStadyService ","start callphone");
- }
- };
-
- protected void onDestroy(){
- super.onDestroy();
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
-
- stopService(intent);
- }
-
-
- }
2.启动服务按钮
这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志
代码如下:
- package lovefang.stadyService;
- import android.app.Service;
- import android.os.IBinder;
- import android.os.Binder;
- import android.content.Intent;
- import android.util.Log;
- public class CountService extends Service{
-
- boolean threadDisable ;
- int count;
-
- public IBinder onBind(Intent intent){
- return null;
- }
- public void onCreate(){
- super.onCreate();
-
- new Thread(new Runnable(){
- public void run(){
- while(!threadDisable){
- try{
- Thread.sleep(1000);
- }catch(InterruptedException e){
-
- }
- count++;
- Log.v("CountService","Count is"+count);
- }
- }
- }).start();
- }
- public void onDestroy(){
- super.onDestroy();
-
- this.threadDisable = true;
- }
- public int getConunt(){
- return count;
- }
- class ServiceBinder extends Binder{
- public CountService getService(){
- return CountService.this;
- }
- }
- }
3.绑定服务
服务有两种实现的方法:
1.startService,启动服务,这时需要程序员管理服务的生命周期
2.bindService,绑定服务,此时Service与Activity绑定在一起
下面是实现的代码:
- package lovefang.stadyService;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
-
- public class UseBrider extends Activity {
-
- CountService countService;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new UseBriderFace(this));
- Intent intent = new Intent(UseBrider.this,CountService.class);
-
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
-
- }
- private ServiceConnection conn = new ServiceConnection(){
-
- public void onServiceConnected(ComponentName name, IBinder service) {
-
- countService = ((CountService.ServiceBinder)service).getService();
-
- }
-
- public void onServiceDisconnected(ComponentName name) {
-
- countService =null;
- }
-
-
- };
- protected void onDestroy(){
- super.onDestroy();
- this.unbindService(conn);
- Log.v("MainStadyServics", "out");
- }
- }
4.发送广播
使用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;
- public class UseBroadcast extends Activity{
-
- private Button sendBroadcast;
-
- 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");
- 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;
-
- public class UseNotification extends Activity {
-
- private Button textButton;
- private Button soundButton;
- private Button vibrateButton;
- private Button ledButton;
- private Button offButton;
- NotificationManager notificationManager;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- getComment();
- registerComment();
- }
-
- public void getComment(){
-
- 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.icon = R.drawable.icon;
- notification.tickerText = "This is text notication";
-
- 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.vibrate = new long[] {0, 100, 200, 300};
- notificationManager.notify(0,notification);
- }
- };
-
- public Button.OnClickListener notificationLed = new Button.OnClickListener(){
- public void onClick(View view){
- Notification notification = new Notification();
- notification.ledOnMS = 300;
- notification.ledOffMS = 1000;
- notificationManager.notify(0,notification);
- }
- };
-
- public Button.OnClickListener notificationOff = new Button.OnClickListener(){
- public void onClick(View view){
- notificationManager.cancel(0);
- }
- };
- }
6.Alarm
闹钟服务
- package lovefang.stadyService;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.app.AlarmManager;
-
- import java.util.Calendar;
-
- public class UseAlarmManager extends Activity {
-
- private Button startAlarm;
- private Button shutdownAlarm;
- private AlarmManager alarm;
-
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usealarmmanager);
- getWidget();
- }
- public void getWidget(){
- startAlarm = (Button)findViewById(R.id.startAlarm);
- shutdownAlarm = (Button)findViewById(R.id.shutDowntAlarm);
- alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
- }
- public void registerWidget(){
- startAlarm.setOnClickListener(startAlarms);
- shutdownAlarm.setOnClickListener(shutdownAlarms);
- }
-
- public Button.OnClickListener startAlarms = new Button.OnClickListener(){
- public void onClick(View view){
-
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- calendar.add(Calendar.SECOND, 10);
-
- alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), null);
- }
- };
- public Button.OnClickListener shutdownAlarms = new Button.OnClickListener(){
- public void onClick(View view){
- alarm.cancel(null);
- }
- };
- }
7.获取手机的状态
这个功能实现的是获取用户手机的一些定义的信息
8.Vibrator
震动功能,实现对手机震动的管理
- package lovefang.stadyService;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.app.Activity;
- import android.view.View;
- import android.content.Context;
- import android.widget.Button;
- public class UseVibrator extends Activity{
-
- private Button vibrator_1_Button;
- private Button vibrator_2_Button;
- private Button vibrator_3_Button;
- private Vibrator vibrator;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.use_vibrator);
- vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
- getWidget();
- registerWidget();
- }
-
- public void getWidget(){
- vibrator_1_Button = (Button) findViewById(R.id.button_vibrator_1);
- vibrator_2_Button = (Button) findViewById(R.id.button_vibrator_2);
- vibrator_3_Button = (Button) findViewById(R.id.button_vibrator_3);
- }
-
- public void registerWidget(){
- vibrator_1_Button.setOnClickListener(vibrator_1);
- vibrator_2_Button.setOnClickListener(vibrator_2);
- vibrator_3_Button.setOnClickListener(vibrator_3);
- }
-
- public Button.OnClickListener vibrator_1 = new Button.OnClickListener(){
- public void onClick(View view){
-
-
-
- vibrator.vibrate(new long[]{100,100}, 0);
- }
- };
-
- public Button.OnClickListener vibrator_2 = new Button.OnClickListener(){
- public void onClick(View view){
- vibrator.vibrate(new long[]{1000,3000,1000,3000}, 0);
- }
- };
-
- public Button.OnClickListener vibrator_3 = new Button.OnClickListener(){
- public void onClick(View view){
- vibrator.vibrate(new long[]{1000,1000,1000,2000,1000,300}, 0);
- }
- };
- }
说明:这个例子实现了Android中常见的许多服务,下面是实现的截图
接下来,以源代码的方式分析这个例子
1.MainActivity--主界面
这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:
- package lovefang.stadyService;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.content.Intent;
- import android.util.Log;
- public class MainStadyServics extends Activity {
-
- Button startServiceButton;
- Button shutDownServiceButton;
- Button startBindServiceButton;
- Button sendBroadcast;
- Button notificationButton;
- Button alarmButton;
- Button handlerButton;
- Button asyncButton;
- Button phoneStateButton;
- Button callphoneButton;
- Button vibratorButton;
- CountService countService;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.v("MainStadyServics", "setContentView");
- setContentView(R.layout.main);
- getWidget();
- regiestListener();
- }
-
- public void getWidget(){
- startServiceButton = (Button)findViewById(R.id.startServerButton);
- startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);
- shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);
- sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
- notificationButton = (Button)findViewById(R.id.notification);
- alarmButton = (Button)findViewById(R.id.alarm);
- handlerButton = (Button)findViewById(R.id.handler);
- asyncButton = (Button)findViewById(R.id.async);
- phoneStateButton = (Button) findViewById(R.id.phonestate);
- callphoneButton = (Button) findViewById(R.id.callphone);
- vibratorButton = (Button) findViewById(R.id.vibrator);
- }
-
- public void regiestListener(){
- startServiceButton.setOnClickListener(startService);
- shutDownServiceButton.setOnClickListener(shutdownService);
- startBindServiceButton.setOnClickListener(startBinderService);
- sendBroadcast.setOnClickListener(broadcastReceiver);
- notificationButton.setOnClickListener(notification);
- alarmButton.setOnClickListener(startAlarm);
- handlerButton.setOnClickListener(handler);
- asyncButton.setOnClickListener(async);
- phoneStateButton.setOnClickListener(phonestate);
- callphoneButton.setOnClickListener(callphoneEvent);
- vibratorButton.setOnClickListener(vibrator);
- }
-
- public Button.OnClickListener startService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
- startService(intent);
- Log.v("MainStadyServics", "start Service");
- }
- };
-
- public Button.OnClickListener shutdownService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
-
- stopService(intent);
- Log.v("MainStadyServics", "shutDown serveice");
- }
- };
-
- public Button.OnClickListener startBinderService = new Button.OnClickListener(){
- public void onClick(View view){
-
- Intent intent = new Intent(MainStadyServics.this,UseBrider.class);
- startActivity(intent);
- Log.v("MainStadyServics", "start Binder Service");
- }
- };
-
- public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);
- startActivity(intent);
- Log.v("MainStadyServics","start broadcast");
- }
- };
-
- public Button.OnClickListener notification = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseNotification.class);
- startActivity(intent);
- Log.v("MainStadyService ","start Notification");
-
- }
- };
-
- public Button.OnClickListener startAlarm = new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);
- startActivity(intent);
- Log.v("MainStadyService ","start alarm");
-
- }
- };
- public Button.OnClickListener handler= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);
- startActivity(intent);
- Log.v("MainStadyService ","start handle");
- }
- };
- public Button.OnClickListener async= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);
- startActivity(intent);
- Log.v("MainStadyService ","start handle");
- }
- };
- public Button.OnClickListener phonestate= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);
- startActivity(intent);
- Log.v("MainStadyService ","start phonestate");
- }
- };
- public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);
- startActivity(intent);
- Log.v("MainStadyService ","start callphone");
- }
- };
- public Button.OnClickListener vibrator= new Button.OnClickListener(){
- public void onClick(View view){
- Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);
- startActivity(intent);
- Log.v("MainStadyService ","start callphone");
- }
- };
-
- protected void onDestroy(){
- super.onDestroy();
- Intent intent = new Intent(MainStadyServics.this,CountService.class);
-
- stopService(intent);
- }
-
-
- }
2.启动服务按钮
这个类实现的是第一个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志
代码如下:
- package lovefang.stadyService;
- import android.app.Service;
- import android.os.IBinder;
- import android.os.Binder;
- import android.content.Intent;
- import android.util.Log;
- public class CountService extends Service{
-
- boolean threadDisable ;
- int count;
-
- public IBinder onBind(Intent intent){
- return null;
- }
- public void onCreate(){
- super.onCreate();
-
- new Thread(new Runnable(){
- public void run(){
- while(!threadDisable){
- try{
- Thread.sleep(1000);
- }catch(InterruptedException e){
-
- }
- count++;
- Log.v("CountService","Count is"+count);
- }
- }
- }).start();
- }
- public void onDestroy(){
- super.onDestroy();
-
- this.threadDisable = true;
- }
- public int getConunt(){
- return count;
- }
- class ServiceBinder extends Binder{
- public CountService getService(){
- return CountService.this;
- }
- }
- }
3.绑定服务
服务有两种实现的方法:
1.startService,启动服务,这时需要程序员管理服务的生命周期
2.bindService,绑定服务,此时Service与Activity绑定在一起
下面是实现的代码:
- package lovefang.stadyService;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
-
- public class UseBrider extends Activity {
-
- CountService countService;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new UseBriderFace(this));
- Intent intent = new Intent(UseBrider.this,CountService.class);
-
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
-
- }
- private ServiceConnection conn = new ServiceConnection(){
-
- public void onServiceConnected(ComponentName name, IBinder service) {
-
- countService = ((CountService.ServiceBinder)service).getService();
-
- }
-
- public void onServiceDisconnected(ComponentName name) {
-
- countService =null;
- }
-
-
- };
- protected void onDestroy(){
- super.onDestroy();
- this.unbindService(conn);
- Log.v("MainStadyServics", "out");
- }
- }
4.发送广播
使用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;
- public class UseBroadcast extends Activity{
-
- private Button sendBroadcast;
-
- 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");
- 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;
-
- public class UseNotification extends Activity {
-
- private Button textButton;
- private Button soundButton;
- private Button vibrateButton;
- private Button ledButton;
- private Button offButton;
- NotificationManager notificationManager;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification);
- getComment();
- registerComment();
- }
-
- public void getComment(){
-
- 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.icon = R.drawable.icon;
- notification.tickerText = "This is text notication";
-
- 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.vibrate = new long[] {0, 100, 200, 300};
- notificationManager.notify(0,notification);
- }
- };
-
- public Button.OnClickListener notificationLed = new Button.OnClickListener(){
- public void onClick(View view){
- Notification notification = new Notification();
- notification.ledOnMS = 300;
- notification.ledOffMS = 1000;
- notificationManager.notify(0,notification);
- }
- };
-
- public Button.OnClickListener notificationOff = new Button.OnClickListener(){
- public void onClick(View view){
- notificationManager.cancel(0);
- }
- };
- }
6.Alarm
闹钟服务
- package lovefang.stadyService;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.app.AlarmManager;
-
- import java.util.Calendar;
-
- public class UseAlarmManager extends Activity {
-
- private Button startAlarm;
- private Button shutdownAlarm;
- private AlarmManager alarm;
-
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.usealarmmanager);
- getWidget();
- }
- public void getWidget(){
- startAlarm = (Button)findViewById(R.id.startAlarm);
- shutdownAlarm = (Button)findViewById(R.id.shutDowntAlarm);
- alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
- }
- public void registerWidget(){
- startAlarm.setOnClickListener(startAlarms);
- shutdownAlarm.setOnClickListener(shutdownAlarms);
- }
-
- public Button.OnClickListener startAlarms = new Button.OnClickListener(){
- public void onClick(View view){
-
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(System.currentTimeMillis());
- calendar.add(Calendar.SECOND, 10);
-
- alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), null);
- }
- };
- public Button.OnClickListener shutdownAlarms = new Button.OnClickListener(){
- public void onClick(View view){
- alarm.cancel(null);
- }
- };
- }
7.获取手机的状态
这个功能实现的是获取用户手机的一些定义的信息
8.Vibrator
震动功能,实现对手机震动的管理
- package lovefang.stadyService;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.app.Activity;
- import android.view.View;
- import android.content.Context;
- import android.widget.Button;
- public class UseVibrator extends Activity{
-
- private Button vibrator_1_Button;
- private Button vibrator_2_Button;
- private Button vibrator_3_Button;
- private Vibrator vibrator;
-
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.use_vibrator);
- vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
- getWidget();
- registerWidget();
- }
-
- public void getWidget(){
- vibrator_1_Button = (Button) findViewById(R.id.button_vibrator_1);
- vibrator_2_Button = (Button) findViewById(R.id.button_vibrator_2);
- vibrator_3_Button = (Button) findViewById(R.id.button_vibrator_3);
- }
-
- public void registerWidget(){
- vibrator_1_Button.setOnClickListener(vibrator_1);
- vibrator_2_Button.setOnClickListener(vibrator_2);
- vibrator_3_Button.setOnClickListener(vibrator_3);
- }
-
- public Button.OnClickListener vibrator_1 = new Button.OnClickListener(){
- public void onClick(View view){
-
-
-
- vibrator.vibrate(new long[]{100,100}, 0);
- }
- };
-
- public Button.OnClickListener vibrator_2 = new Button.OnClickListener(){
- public void onClick(View view){
- vibrator.vibrate(new long[]{1000,3000,1000,3000}, 0);
- }
- };
-
- public Button.OnClickListener vibrator_3 = new Button.OnClickListener(){
- public void onClick(View view){
- vibrator.vibrate(new long[]{1000,1000,1000,2000,1000,300}, 0);
- }
- };
- }
一个Demo学完Android中所有的服务(转)
标签:
原文地址:http://www.cnblogs.com/jetereting/p/4438428.html