扫一扫,加我们为好友有惊喜哦~
-->

核心课程
开班动态
专题服务

您当前所在位置:首页 > 中心动态 >>
Android开发之Service实例——常州电脑培训

 

Service是运行在后台的服务程序,一般不和用户交互,因此,没有可视化界面。定义一个Service只需要继承Service类。
       Service必须在AndroidManifest.xml中声明。
      
1、
Activity中声明按钮启动、停止、绑定、解除绑定Service
public class MyActivity extends Activity {
   
    private Button startBtn,stopBtn,bindBtn,unbindBtn;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        this.startBtn = (Button) findViewById(R.id.button1);
        this.stopBtn = (Button) findViewById(R.id.button2);
        this.bindBtn = (Button) findViewById(R.id.button3);
        this.unbindBtn = (Button) findViewById(R.id.button4);
       
        //启动Service
        startBtn.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent();
              intent.setAction("com.myservice.service.MY_SERVICE");
              startService(intent);
           }
       });
       
        //停止Service
        stopBtn.setOnClickListener(new View.OnClickListener(){
 
           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent();
              intent.setAction("com.myservice.service.MY_SERVICE");
              stopService(intent);
           }
       
        });
       
   
        bindBtn.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent();
              intent.setAction("com.myservice.service.MY_SERVICE");
              bindService(intent, conn, Service.BIND_AUTO_CREATE);
           }
       });
       
        unbindBtn.setOnClickListener(new View.OnClickListener() {
          
           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent();
              intent.setAction("com.myservice.service.MY_SERVICE");
              unbindService(conn);
           }
       });
    }
   
   
   
    //连接对象
    private ServiceConnection conn = new ServiceConnection(){
 
       @Override
       public void onServiceConnected(ComponentName cn, IBinder binder) {
           Log.i("Service", "连接成功!");
           Toast.makeText(MyActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
       }
      
 
       @Override
       public void onServiceDisconnected(ComponentName arg0) {
           Log.i("Service", "断开连接!");
           Toast.makeText(MyActivity.this,"断开连接",Toast.LENGTH_SHORT).show();
       }
   
    };
}
声明Service:
public class MyService extends Service{
 
    @Override
    public IBinder onBind(Intent arg0) {
       Log.i("Service", "onBind......");
       Toast.makeText(this, "onBind...", Toast.LENGTH_SHORT).show();
       return null;
    }
   
    @Override
    public void onCreate() {
       // TODO Auto-generated method stub
       Log.i("Service", "onCreate......");
       Toast.makeText(this, "onCreate...", Toast.LENGTH_SHORT).show();
    }
   
    @Override
    public void onStart(Intent intent, int startId) {
       // TODO Auto-generated method stub
       Log.i("Service", "onStart......");
       Toast.makeText(this, "onStart...", Toast.LENGTH_SHORT).show();
    }
   
    @Override
    public void onDestroy() {
       Log.i("Service", "onDestory......");
       Toast.makeText(this, "onDestory...", Toast.LENGTH_SHORT).show();
    }
 
}
 
在Manifest.xml中声明:
        <service android:name="com.mysevice.service.MyService">
            <intent-filter>
                <action android:name="com.myservice.service.MY_SERVICE"></action>
            </intent-filter>
        </service>
 

main.xml中声明四个按钮

 

更多精彩:常州ps培训


精彩文章推荐
常州电脑培训——栈和堆有什么区别?[2012-6-15]
【东方博宜软件培训】软件开发可行性分析案例可行性分析案例之一[2012-6-13]
常见的排序算法[2012-6-12]
【常州电脑培训】编写Java应用程序[2012-6-7]
【常州电脑培训】Java的数据类型及方法调用[2012-6-7]
做淘宝直通车有用吗?[2012-4-19]
常州电脑培训|常州室内设计培训--磁盘碎片整理过程[2011-10-31]
常州电脑培训-电脑学习快速入门三步走[2011-10-10]


返回顶部