package com.aidl.main;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.aidl.Utils;
import com.aidl.entity.Book;
import com.aidl.entity.BookManager;
import java.util.List;
/*
* http://blog.csdn.net/luoyanglizi/article/details/51980630
* http://blog.csdn.net/luoyanglizi/article/details/52029091
*/
public class MainActivity extends AppCompatActivity {
private static final String TAG = "AIDL2222";
//由AIDL文件生成的Java类
private BookManager mBookManager = null;
//标志当前与服务端连接状况的布尔值,false为未连接,true为连接中
private boolean isOnBind = false;
//包含Book对象的list
private List<Book> mBooks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private static int verson = 1;
public void addBook(View view) {
boolean isStart = Utils.isServiceWork(this, "com.aidl.Service.AIDLService");
Log.i(TAG, "addBook: isStart = " + isStart);
if (!isOnBind) {
attemptToBindService();
Toast.makeText(this, "当前与服务端处于未连接状态,正在尝试重连,请稍后再试", Toast.LENGTH_SHORT).show();
return;
}
if (mBookManager == null)
return;
Book book = new Book();
book.setName("APP研发宝典" + verson);
verson++;
book.setPrice(30 + verson);
Log.i(TAG, "Activity addBook: " + book.toString());
try {
mBookManager.addBook(book);
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getBooks(View view) {
if (!isOnBind) {
attemptToBindService();
Toast.makeText(this, "当前与服务端处于未连接状态,正在尝试重连,请稍后再试", Toast.LENGTH_SHORT).show();
return;
}
if (mBookManager == null)
return;
try {
List<Book> books = mBookManager.getBooks();
Log.i(TAG, "getBooks: " + books.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void attemptToBindService() {
Log.i(TAG, "attemptToBindService: ");
/* 显示启动Service,行
Intent intent = new Intent(MainActivity.this,AIDLService.class);
Log.i(TAG, "bindService()");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
*/
final Intent intent = new Intent();
intent.setAction("com.aidl.Service.AIDLService");
// intent.setPackage("com.aidl.Service");//这样是不行的,要使用下面eintent
final Intent eintent = new Intent(Utils.createExplicitFromImplicitIntent(this, intent));
bindService(eintent, mServiceConnection, Service.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected: ");
mBookManager = BookManager.Stub.asInterface(service);
isOnBind = true;
if (mBookManager != null) {
try {
mBooks = mBookManager.getBooks();
Log.i(TAG, "onServiceConnected: " + mBooks.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected: ");
isOnBind = false;
}
};
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
AIDL.rar (67个子文件)
AIDL
gradlew 5KB
settings.gradle 16B
.idea
libraries
junit_4_12.xml 477B
support_compat_24_2_0.xml 976B
support_v4_24_2_0.xml 420B
appcompat_v7_24_2_0.xml 788B
support_media_compat_24_2_0.xml 1018B
support_annotations_24_2_0.xml 549B
support_core_ui_24_2_0.xml 983B
hamcrest_core_1_3.xml 526B
animated_vector_drawable_24_2_0.xml 672B
support_fragment_24_2_0.xml 990B
support_vector_drawable_24_2_0.xml 667B
support_core_utils_24_2_0.xml 822B
runConfigurations.xml 564B
misc.xml 3KB
compiler.xml 686B
workspace.xml 188KB
gradle.xml 739B
.name 4B
encodings.xml 159B
modules.xml 345B
copyright
profiles_settings.xml 76B
build
intermediates
dex-cache
cache.xml 64B
generated
mockable-android-24.jar 31.96MB
gradle
wrapper
gradle-wrapper.properties 231B
gradle-wrapper.jar 52KB
.gradle
2.10
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 20KB
localClassSetAnalysis.lock 17B
taskArtifacts
cache.properties.lock 17B
fileHashes.bin 129KB
outputFileStates.bin 20KB
taskArtifacts.bin 104KB
fileSnapshots.bin 1.94MB
cache.properties 31B
local.properties 456B
gradlew.bat 2KB
gradle.properties 838B
.gitignore 105B
app
src
androidTest
java
com
aidl
main
ApplicationTest.java 356B
test
java
com
aidl
main
ExampleUnitTest.java 320B
main
AndroidManifest.xml 1KB
res
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-mdpi
ic_launcher.png 2KB
drawable
layout
activity_main.xml 965B
values-w820dp
dimens.xml 364B
values
colors.xml 214B
strings.xml 70B
styles.xml 394B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
aidl
com
aidl
entity
BookManager.aidl 803B
Book.aidl 546B
java
com
aidl
entity
Book.java 2KB
Service
AIDLService.java 2KB
Utils.java 2KB
main
MainActivity.java 4KB
libs
app.iml 10KB
.gitignore 8B
build.gradle 759B
proguard-rules.pro 689B
AIDL.iml 935B
build.gradle 525B
共 67 条
- 1
资源评论
- 企鹅啊但是我2017-04-18有点乱不太适合学习
gdliweibing
- 粉丝: 184
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功