服务
-
Service创建:
- 创建类并继承Service类
- 重写
onCreate()
、onStartCommand()
、onDestroy()
方法 - 在Activity中调用
startService(new Intent(MusicServiceActivity.this, MusicService.class));
和stopService(new Intent(MusicServiceActivity.this, MusicService.class));
启动和停止Service。 - 在AndroidManifest注册
<service android:name=".MusicService"/>
。 -
Bound Service创建:
BoundService和Service的区别:BoundService可以通过get到的Service对象与Service进行数据沟通。
- 创建 ServiceConnection对象并重写方法
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicService.LocalBinder binder = (MusicService.LocalBinder) service;
mMusicService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
- 将ServiceConnection对象传给
bindService()
bindService(new Intent(MusicServiceActivity.this, MusicService.class), mServiceConnection, BIND_AUTO_CREATE);
- 在Service内创建一个内部类并继承Binder,定义一个方法返回本Service对象,并在onBind()方法中返回内部类对象。
@Nullable
@Override
public IBinder onBind(Intent intent) {
// 返回给ServiceConnection对象的onServiceConnected()内
return new LocalBinder();
}
public class LocalBinder extends Binder{
// 获取当前Service
MusicService getService(){
return MusicService.this;
}
-
IntentService
广播(BroadcastReceiver)
-
BroadcastReceiver两种注册方式:
- 静态注册,在Manifest中注册:
<receiver android:name=".TestBroadcastReceiver">
<intent-filter>
<action android:name="com.example.hx.demo.broadcast"/>
</intent-filter>
</receiver>
2.动态注册,创建一个Activity:
public class SendBroadcastActivity extends AppCompatActivity {
private Button mButton;
private TestBroadcastReceiver mTestBroadcastReceiver = new TestBroadcastReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_broadcast);
setTitle(getIntent().getStringExtra("appName"));
mButton = (Button) findViewById(R.id.send_broadcast);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.hx.demo.broadcast");
intent.putExtra("toast", "this is my toast of brodcast");
sendBroadcast(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
// 动态注册广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.hx.demo.broadcast");
registerReceiver(mTestBroadcastReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
// 反注册
unregisterReceiver(mTestBroadcastReceiver);
}
}
-
在广播接受端:
public class TestBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 不能做耗时操作,ANR
if (intent != null) {
if (TextUtils.equals(intent.getAction(), "com.example.hx.demo.broadcast")){
Toast.makeText(context, intent.getStringExtra("toast"), Toast.LENGTH_SHORT).show();
}
}
}
}
1.有序性广播:
sendOrderedBroadcast();
2.本地广播:LocalBroadcastManager
WebView
1.使用之前需要获取联网权限。
2.加载线上url或本地html
3.和JS进行交互、历史记录、导航......
public class WebViewActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
setTitle(getIntent().getStringExtra("appName"));
mWebView = (WebView) findViewById(R.id.web_view);
// 加载网址
mWebView.loadUrl("http://www.baidu.com");
// 允许使用JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);
// 可重写很多方法实现不同的功能
mWebView.setWebViewClient(new WebViewClient(){
});
}
@Override
public void onBackPressed() {
if (mWebView != null && mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Widget
Widget是BroadcastReceive的实现,由于Widget和App是相分离的,所以Widget是运行再主屏幕进程上的,所以和传统的Activity的一些设置有很大不同,有很多限制。
Android Widget详解
Google Developers App Widgets