码迷,mamicode.com
首页 > 移动开发 > 详细

Android 接口定义Demo

时间:2016-06-17 08:29:11      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

技术分享技术分享技术分享

 1 // IAppServiceRemoteBinder.aidl
 2 package com.example.metrox.l16;
 3 
 4 // Declare any non-default types here with import statements
 5 
 6 interface IAppServiceRemoteBinder {
 7     /**
 8      * Demonstrates some basic types that you can use as parameters
 9      * and return values in AIDL.
10      */
11     void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
12             double aDouble, String aString);
13 
14      void setData(String data);
15 }
 1 package com.example.metrox.l16;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 import android.os.RemoteException;
 7 import android.provider.Settings;
 8 
 9 public class AppService extends Service {
10 
11 
12     public AppService() {
13     }
14 
15     @Override
16     public IBinder onBind(Intent intent) {
17         return new IAppServiceRemoteBinder.Stub() {
18             @Override
19             public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
20 
21             }
22 
23             @Override
24             public void setData(String data) throws RemoteException {
25                  AppService.this.data = data;
26             }
27         };
28     }
29 
30     @Override
31     public int onStartCommand(Intent intent, int flags, int startId) {
32         return super.onStartCommand(intent, flags, startId);
33     }
34 
35     @Override
36     public void onCreate() {
37         super.onCreate();
38         System.out.println("Service OnCreate");
39         new Thread(){
40             @Override
41             public void run() {
42                 super.run();
43                 isRuning = true;
44                 while (isRuning){
45                     System.out.println(data);
46                     try {
47                         sleep(1000);
48                     } catch (InterruptedException e) {
49                         e.printStackTrace();
50                     }
51                 }
52             }
53         }.start();
54     }
55 
56     @Override
57     public void onDestroy() {
58         super.onDestroy();
59         System.out.println("Service OnDestroy");
60     }
61 
62     private String data = "默认数据";
63     private boolean isRuning = false;
64 }
 1 package com.example.metrox.l16;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 
 7 public class MainActivity extends AppCompatActivity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         startService(new Intent(this,AppService.class));
14     }
15 
16     @Override
17     protected void onDestroy() {
18         super.onDestroy();
19         stopService(new Intent(this,AppService.class));
20     }
21 }
 1 package com.example.testapp;
 2 
 3 import android.content.ComponentName;
 4 import android.content.Intent;
 5 import android.content.ServiceConnection;
 6 import android.os.IBinder;
 7 import android.os.RemoteException;
 8 import android.support.v7.app.AppCompatActivity;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.EditText;
12 import android.widget.TextView;
13 
14 import com.example.metrox.l16.IAppServiceRemoteBinder;
15 
16 public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
17 
18     private TextView textView;
19     private Intent intent;
20     private EditText et;
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         intent = new Intent();
26         intent.setComponent(new ComponentName("com.example.metrox.l16","com.example.metrox.l16.AppService"));
27         textView = (TextView) findViewById(R.id.textView);
28         findViewById(R.id.btnStartAppService).setOnClickListener(this);
29         findViewById(R.id.btnStopAppService).setOnClickListener(this);
30         findViewById(R.id.btnBindAppService).setOnClickListener(this);
31         findViewById(R.id.btnUnBindAppService).setOnClickListener(this);
32         findViewById(R.id.btnSyncData).setOnClickListener(this);
33         et =(EditText) findViewById(R.id.editText);
34     }
35 
36     @Override
37     public void onClick(View view) {
38         switch (view.getId()) {
39             case R.id.btnStartAppService:
40                 startService(intent);
41                 break;
42             case R.id.btnStopAppService:
43                 stopService(intent);
44                 break;
45             case R.id.btnBindAppService:
46                 bindService(intent,this,BIND_AUTO_CREATE);
47                 break;
48             case R.id.btnUnBindAppService:
49                 unbindService(this);
50                 binder = null;
51                 break;
52             case R.id.btnSyncData:
53                 if(binder != null){
54                     try {
55                         binder.setData(et.getText().toString());
56                     } catch (RemoteException e) {
57                         e.printStackTrace();
58                     }
59                 }
60                 break;
61         }
62     }
63 
64     @Override
65     public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
66         System.out.println("Binder Service");
67         System.out.println(iBinder);
68         binder = IAppServiceRemoteBinder.Stub.asInterface(iBinder);
69     }
70 
71     @Override
72     public void onServiceDisconnected(ComponentName componentName) {
73 
74     }
75 
76     private IAppServiceRemoteBinder binder = null;
77 }

技术分享

Android 接口定义Demo

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5592703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!