zoukankan      html  css  js  c++  java
  • 好记性不如烂笔杆android学习笔记<十一> Service的应用

    23,//Service的应用
    <1>//manifest.xml对service注册
    <service android:name=".FirstService"></service>
    <2>//Java文件

     1 public class TestActivity extends Activity {
     2     private Button startServiceButton = null;
     3     private Button stopServiceButton = null;
     4     @Override
     5     public void onCreate(Bundle savedInstanceState) {
     6         super.onCreate(savedInstanceState);
     7         setContentView(R.layout.main);
     8         startServiceButton = (Button)findViewById(R.id.startServiceButton);
     9         startServiceButton.setOnClickListener(new StartServiceListener());
    10         stopServiceButton = (Button)findViewById(R.id.stopServiceButton);
    11         stopServiceButton.setOnClickListener(new StopServiceListener());
    12         System.out.println("Activity onCreate ");
    13     }
    14     class StartServiceListener implements OnClickListener{
    15 
    16         @Override
    17         public void onClick(View v) {
    18             Intent intent = new Intent();
    19             intent.setClass(TestActivity.this,FirstService.class);
    20             startService(intent);
    21         }
    22     }
    23     class StopServiceListener implements OnClickListener{
    24 
    25         @Override
    26         public void onClick(View v) {
    27             Intent intent = new Intent();
    28             intent.setClass(TestActivity.this,FirstService.class);
    29             stopService(intent);
    30         }
    31     }
    32 }

     

    <3>//创建Service文件

     1 public class FirstService extends Service{
     2     @Override
     3     public IBinder onBind(Intent arg0) {
     4         System.out.println("Service onBind ");
     5         return null;
     6     }
     7     //当创建一个Service对象之后,会先调用这个函数
     8     @Override
     9     public void onCreate() {
    10         super.onCreate();
    11         System.out.println("Service onCreate ");
    12     }
    13 
    14     @Override
    15     public void onDestroy() {
    16         super.onDestroy();
    17         System.out.println("Service onDestory ");
    18     }
    19 
    20     @Override
    21     public int onStartCommand(Intent intent, int flags, int startId) {
    22         System.out.println("flags--->" + flags);
    23         System.out.println("startId--->" + startId);
    24         System.out.println("Service onStartCommand");
    25         return START_NOT_STICKY;
    26     }
    27     
    28 }
  • 相关阅读:
    【OS_Windows】用微pe制作启动盘安装操作系统
    技术列表
    RPC 的概念模型与实现解析
    asp.net站点阻止某个文件夹或者文件被浏览器访问
    常用插件
    安全相关
    asp.net mvc 请求处理流程,记录一下。
    接口的显示实现和隐式实现
    值类型与引用类型的简单测试,没有太多的理论,一目了然。
    IEnumerable、GetEnumerator、IEnumerator之间的关系。
  • 原文地址:https://www.cnblogs.com/zjqlogs/p/2780220.html
Copyright © 2011-2022 走看看