Skip to content

Commit

Permalink
添加一处点击事件
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeKing committed Jun 12, 2016
1 parent 7a0e87d commit 7b271ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
38 changes: 26 additions & 12 deletions app/src/main/java/org/wangchenlong/wcl_aidl_demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MainActivity extends AppCompatActivity {

private static final int MESSAGE_NEW_BOOK_ARRIVED = 1;

private TextView mTextView;
private TextView mTvBookList;

private IBookManager mRemoteBookManager;

Expand All @@ -51,7 +51,6 @@ public class MainActivity extends AppCompatActivity {
private ServiceConnection mConnection = new ServiceConnection() {
@Override public void onServiceConnected(ComponentName name, IBinder service) {
IBookManager bookManager = IBookManager.Stub.asInterface(service);

try {
mRemoteBookManager = bookManager;
Book newBook = new Book(3, "学姐的故事");
Expand All @@ -73,9 +72,8 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.main_tv_book_list);
Intent intent = new Intent(this, BookManagerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
mTvBookList = (TextView) findViewById(R.id.main_tv_book_list);

}

@Override protected void onDestroy() {
Expand All @@ -87,10 +85,24 @@ protected void onCreate(Bundle savedInstanceState) {
e.printStackTrace();
}
}
unbindService(mConnection);
try {
unbindService(mConnection);
} catch (Exception e) {
e.printStackTrace();
}
super.onDestroy();
}

/**
* 绑定服务按钮的点击事件
*
* @param view 视图
*/
public void bindService(View view) {
Intent intent = new Intent(this, BookManagerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

/**
* 获取图书数量的异步线程, 直接绑定点击事件
*
Expand Down Expand Up @@ -127,7 +139,7 @@ private class BookListAsyncTask extends AsyncTask<Void, Void, List<Book>> {
for (int i = 0; i < books.size(); ++i) {
content += books.get(i).toString() + "\n";
}
mTextView.setText(content);
mTvBookList.setText(content);
}
}

Expand All @@ -138,11 +150,13 @@ private class BookListAsyncTask extends AsyncTask<Void, Void, List<Book>> {
*/
private int getListNum() {
int num = 0;
try {
List<Book> list = mRemoteBookManager.getBookList();
num = list.size();
} catch (RemoteException e) {
e.printStackTrace();
if (mRemoteBookManager != null) {
try {
List<Book> list = mRemoteBookManager.getBookList();
num = list.size();
} catch (RemoteException e) {
e.printStackTrace();
}
}
return num;
}
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
android:onClick="getBookList"
android:text="获取图书数量"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bindService"
android:text="绑定服务"/>

<TextView
android:id="@+id/main_tv_book_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="getBookList"
android:text="获取图书列表"/>
android:text="显示图书列表(绑定服务)"/>
</LinearLayout>

0 comments on commit 7b271ed

Please sign in to comment.