标签:android style blog http color io 使用 java ar
在Android中,Service是运行在主线程中的,如果在Service中处理一些耗时的操作,就会导致程序出现ANR.
package com.example.servicetest.MyAIDLService;
interface MyAIDLService {
int plus(int a, int b);
String toUpperCase(String str);
}
public class MainActivity extends Activity implements OnClickListener {
Button btn_start,btn_stop,btn_bind,btn_unbind;
MyAIDLService mAIDLService;
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAIDLService=MyAIDLService.Stub.asInterface(service);
try {
int result=mAIDLService.plus(8, 7);
String upperStr = mAIDLService.toUpperCase("hello world");
Log.d("info", "--result---" + result);
Log.d("info", "--upperStr---" + upperStr);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start=(Button) findViewById(R.id.btn_start);
btn_stop=(Button) findViewById(R.id.btn_stop);
btn_bind=(Button) findViewById(R.id.btn_bind);
btn_unbind=(Button) findViewById(R.id.btn_unBind);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_bind.setOnClickListener(this);
btn_unbind.setOnClickListener(this);
Log.e("info", "process id is " + Process.myPid());
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_start){
Intent intent=new Intent(this,MyService.class);
startService(intent);
}
else if(v.getId()==R.id.btn_stop){
Intent intent=new Intent(this, MyService.class);
stopService(intent);
}
else if(v.getId()==R.id.btn_bind){
Intent intent=new Intent(this, MyService.class);
bindService(intent, connection,BIND_AUTO_CREATE);
}
else if(v.getId()==R.id.btn_unBind){
unbindService(connection);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Bind Service"
/>
</RelativeLayout>
public class MainActivity extends Activity {
private MyAIDLService mAIDLService;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mAIDLService=MyAIDLService.Stub.asInterface(service);
try {
int result=mAIDLService.plus(50, 48);
String upperStr=mAIDLService.toUpperCase("hello world");
Log.e("info","---result---"+result);
Log.e("info","upperStr--"+upperStr);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("com.example.myAIDL");
bindService(intent, connection, BIND_AUTO_CREATE);
}
});
}
}
标签:android style blog http color io 使用 java ar
原文地址:http://www.cnblogs.com/aibuli/p/f9ef6186e19000705734774965a777b5.html