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

这篇博客通过一个实例详细介绍了Android中服务的使用,包括启动服务、绑定服务、广播接收器、通知和闹钟服务等。文章通过源代码分析,帮助读者理解Android服务的生命周期和不同启动方式,同时涉及了震动管理和通知栏的运用。
摘要由CSDN通过智能技术生成

注:这个例子来自“安卓巴士”,经阅读,理解,写此文章


说明:这个例子实现了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;// 使用handler
	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);
    			/**退出Activity是,停止服务*/
    		stopService(intent);
    		Log.v("MainStadyServics", "shutDown serveice");
    	}
    };
    	/**打开绑定服务的Activity*/
    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);
   
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值