- 通过intent传递数据
- 通过IBinder传递数据
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.gst.user.application.TempActivity"
tools:showIn="@layout/activity_temp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginTop="48dp"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启服务"
android:onClick="onClick_startService"
android:id="@+id/button"
android:layout_marginTop="32dp"
android:layout_below="@+id/editText"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="onClick_stopService"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="onClick_bindService"
android:id="@+id/button3"
android:layout_marginTop="32dp"
android:layout_below="@+id/button2"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除绑定"
android:onClick="onClick_unbindService"
android:id="@+id/button4"
android:layout_marginTop="37dp"
android:layout_below="@+id/button3"
android:layout_alignStart="@+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同步数据"
android:onClick="onClick_syncInfo"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_alignStart="@+id/button4"
android:layout_marginTop="22dp" />
</RelativeLayout>
MyService类
package com.gst.user.application;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
private boolean isRunning=false;
private String data="这是默认信息";
private Callback callback=null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new MyBinder();
}
public class MyBinder extends Binder{
public void setData(String data){
MyService.this.data=data;
}
public MyService getService(){
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
isRunning=true;
new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while (isRunning){
i++;
String s=i+":"+data;
Log.d(TAG,i+":"+data);
if (callback!=null){
callback.onDataChange(s);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public Callback getCallback() {
return callback;
}
public void setCallback(Callback callback) {
this.callback = callback;
}
public interface Callback{
void onDataChange(String data);
}
}
主程序
package com.gst.user.application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class TempActivity extends AppCompatActivity {
private static final String TAG = "TempActivity";
private Intent intent;
TextView textView;
MyService.MyBinder binder;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
intent = new Intent(this, MyService.class);
textView = (TextView) findViewById(R.id.textView3);
editText=(EditText)findViewById(R.id.editText);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x101) {
textView.setText(msg.obj.toString());
}
}
};
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "ServiceConnection:onServiceConnected");
binder = (MyService.MyBinder) service;
binder.getService().setCallback(new MyService.Callback() {
@Override
public void onDataChange(String data) {
Message message = handler.obtainMessage();
message.what = 0x101;
message.obj = data;
handler.sendMessage(message);
}
});
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "ServiceConnection:onServiceDisconnected");
}
};
public void onClick_startService(View view) {
startService(intent);
}
public void onClick_stopService(View view) {
stopService(intent);
}
public void onClick_bindService(View view) {
bindService(intent, connection, BIND_AUTO_CREATE);
}
public void onClick_unbindService(View view) {
if (connection != null) {
unbindService(connection);
}
}
public void onClick_syncInfo(View view) {
if (binder != null) {
String newData=editText.getText().toString().trim();
if (newData.equals("")){
return;
}
binder.setData(newData);
}
}
}
2.Android 中 AIDL 的理解与使用
- 跨应用启动 Service
intent.setComponent(new ComponentName("包名","服务名"));
startService(intent);
3.Android 广播接收器 BroadcastReceiver
4.Android 日志系统
- 使用日志API
System.out、System.err、Log.v、Log.d、Log.i、Log.w、Log.e 的使用
- 对日志进行分类
对日志进行分类呈现,便于开发调试
- 在DDMS中查看日志
使用 Android Studio 中的 DDMS 以及独立的 DDMS 查看日志
5.Android 权限系统
- 定义权限
<permission android:name="com.gst.user.application.permission"/>
- 在代码中做权限检查
int checkResult=this.checkCallingOrSelfPermission("android.permission.VIBRATE");
if (checkResult!= PackageManager.PERMISSION_GRANTED){
throw new SecurityException("需要申请权限:android.permission.VIBRATE");
}
- 为基本组件添加权限检查