Service学习——第一篇(创建简单的Service)

Service学习之——第一篇

创建一个简单的Service

通过这一篇的学习,你应该知道怎么创建一个简单的Service,并学会了启动和停止它的一些方法。startService(Intent intent); stopService(Intent intent);

main.xml布局文件:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

  

   <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/start"

        android:text="开启服务"

        />

   <Button

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/stop"

        android:text="停止服务"

        />

</LinearLayout>

MyService

packagecom.example.myservice;

 

importandroid.app.Service;

importandroid.content.Intent;

importandroid.os.IBinder;

importandroid.widget.Toast;

 

public classMyService extends Service{

 

                       public MyService() {

                            // TODOAuto-generated constructor stub

                       }

 

                       @Override

                       public IBinderonBind(Intent arg0) {

                            // TODOAuto-generated method stub

                            return null;

                       }

                      

                       @Override

                       public intonStartCommand(Intent intent, int flags, int startId) {

                            // TODOAuto-generated method stub

                            //我们希望这个服务在被显示停止前一直运行,所以返回“粘性的”状态

                            Toast.makeText(this,"Service Started", Toast.LENGTH_LONG).show();

                            return START_STICKY;

                       }

                       @Override

                       public void onDestroy() {

                            // TODOAuto-generated method stub

                            super.onDestroy();

                            Toast.makeText(this,"Service Stopped", Toast.LENGTH_LONG).show();

                       }

}

MainActivity类

packagecom.example.myservice;

 

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

 

public classMainActivity extends Activity {

                       private Button start;

                       private Button stop;

                       @Override

                       protected voidonCreate(Bundle savedInstanceState) {

                            super.onCreate(savedInstanceState);

                            setContentView(R.layout.activity_main);

                            start = (Button)findViewById(R.id.start);

                            stop = (Button)findViewById(R.id.stop);

                            start.setOnClickListener(newOnClickListener() {

                                    

                                     @Override

                                     public voidonClick(View v) {

                                               //TODO Auto-generated method stub

                                               Intentintent = new Intent(MainActivity.this,MyService.class);

                                               startService(intent);

                                     }

                            });

                            stop.setOnClickListener(newOnClickListener() {

                                    

                                     @Override

                                     public voidonClick(View v) {

                                               //TODO Auto-generated method stub

                                               stopService(newIntent(getBaseContext(),MyService.class));

                                     }

                            });

                       }

 

                       @Override

                       public booleanonCreateOptionsMenu(Menu menu) {

                            // Inflate the menu;this adds items to the action bar if it is present.

                            getMenuInflater().inflate(R.menu.main,menu);

                            return true;

                       }

}

 

在Manifest中配置Service

<service android:name="com.example.myservice.MyService"></service>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值