151102笔记
来源:互联网 发布:java判断全角字符 编辑:程序博客网 时间:2024/06/04 23:26
Android studio AIDL
首先右键建立AIDL
package com.example.administrator.talkapp;// Declare any non-default types here with import statementsinterface MyAIDLService { int plus(int a, int b); String toUpperCase(String str);}
注意那个包名
来到清单,要保持包名一致
然后build-makeproject,就可以看到
然后就可以开始启用了
package com.example.administrator.talkapp;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.widget.Button;/** * Created by Administrator on 2015/11/2. */public class MainActivity extends Activity implements View.OnClickListener{ private Button startService; private Button stopService; private Button bindService; private Button unbindService; private MyService.MyBinder myBinder; private MyAIDLService myServiceAIDL; private Intent binderIntent; private final static boolean create_flag=true; private final static boolean destory_flag=false; private final static String TAG="MainActivity"; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) {// myBinder=(MyService.MyBinder)iBinder;// myBinder.startDownload(); myServiceAIDL=MyAIDLService.Stub.asInterface(iBinder); try{ //用过AIDL远程调用 Log.e("fish","aidl"); myServiceAIDL.toUpperCase("lala"); }catch (Exception e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2);// startService = (Button) findViewById(R.id.start_service);// stopService = (Button) findViewById(R.id.stop_service);// bindService = (Button) findViewById(R.id.bind_service);// unbindService = (Button) findViewById(R.id.unbind_service);// startService.setOnClickListener(this);// stopService.setOnClickListener(this);// bindService.setOnClickListener(this);// unbindService.setOnClickListener(this); //开启服务 Intent intent = new Intent(this, MyService.class); startService(intent); //连接远程Service和Activity binderIntent = new Intent(this,MyService.class); Bundle bundle = new Bundle(); bundle.putBoolean("flag", create_flag); binderIntent.putExtras(bundle); bindService(binderIntent, connection, BIND_AUTO_CREATE); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start_service: Intent startIntent = new Intent(this, MyService.class); startService(startIntent); break; case R.id.stop_service: Intent stopIntent = new Intent(this, MyService.class); stopService(stopIntent); break; case R.id.bind_service: Intent bindIntent = new Intent(this, MyService.class); bindService(bindIntent, connection, BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(connection); break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"++MainActivity onDestroy++"); boolean flag = false; //暂停服务 Intent intent = new Intent(this, MyService.class); stopService(intent); //断开与远程Service的连接 unbindService(connection); }}
package com.example.administrator.talkapp;import android.app.Notification;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;/** * Created by Administrator on 2015/11/2. */public class MyService extends Service {// private MyBinder mBinder = new MyBinder(); boolean flag; private final static String TAG = "MainService";MyAIDLService.Stub mbinder=new MyAIDLService.Stub() { @Override public int plus(int a, int b) throws RemoteException { return 0; } @Override public String toUpperCase(String str) throws RemoteException { return null; }}; @Override public void onCreate() { super.onCreate();// Log.d(TAG, "onCreate() executed");// Notification notification = new Notification(R.drawable.ic_check,// "有通知到来", System.currentTimeMillis());// Intent notificationIntent = new Intent(this, MainActivity.class);// PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,// notificationIntent, 0);//// notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容",//// pendingIntent);//// startForeground(1, notification);// Notification n = new Notification.Builder(this)// .setContentTitle("有通知到来")// .setTicker("Ticker")// .setContentText("内容")// .setSmallIcon(R.drawable.ic_check)// .setContentIntent(pendingIntent)// .build();//// startForeground(1, n); try{ Thread.sleep(1000); }catch (Exception e) { e.printStackTrace(); } Log.d(TAG, "onCreate() executed"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand() executed"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy() executed"); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } class MyBinder extends Binder { public void startDownload() { Log.d("TAG", "startDownload() executed"); // 执行具体的下载任务 } }}
0 0
- 151102笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 笔记
- 设计模式之--装饰者模式
- HDU 5534 Partial Tree (2015长春站H题&&完全背包)
- OC03 避免互相引入 方法理解5
- Maven多模块布局实例详解
- 关于手机端手势js库的选择:hammer.js
- 151102笔记
- IP地址 网段的划分
- cocos-js 的事件监听(学习)
- BFS Codeforces591E Three States
- OC04 NSString、NSArray、NSNumber
- pods的更新
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- OC04 NSString、NSArray、NSNumber-2
- 2023: [Usaco2005 Nov]Ant Counting 数蚂蚁