服務(wù)
-
Service創(chuàng)建:
- 創(chuàng)建類并繼承Service類
- 重寫
onCreate()
总棵、onStartCommand()
、onDestroy()
方法 - 在Activity中調(diào)用
startService(new Intent(MusicServiceActivity.this, MusicService.class));
和stopService(new Intent(MusicServiceActivity.this, MusicService.class));
啟動(dòng)和停止Service改含。 - 在AndroidManifest注冊(cè)
<service android:name=".MusicService"/>
情龄。 -
Bound Service創(chuàng)建:
BoundService和Service的區(qū)別:BoundService可以通過(guò)get到的Service對(duì)象與Service進(jìn)行數(shù)據(jù)溝通。
- 創(chuàng)建 ServiceConnection對(duì)象并重寫方法
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對(duì)象傳給
bindService()
bindService(new Intent(MusicServiceActivity.this, MusicService.class), mServiceConnection, BIND_AUTO_CREATE);
- 在Service內(nèi)創(chuàng)建一個(gè)內(nèi)部類并繼承Binder捍壤,定義一個(gè)方法返回本Service對(duì)象骤视,并在onBind()方法中返回內(nèi)部類對(duì)象。
@Nullable
@Override
public IBinder onBind(Intent intent) {
// 返回給ServiceConnection對(duì)象的onServiceConnected()內(nèi)
return new LocalBinder();
}
public class LocalBinder extends Binder{
// 獲取當(dāng)前Service
MusicService getService(){
return MusicService.this;
}
-
IntentService
廣播(BroadcastReceiver)
-
BroadcastReceiver兩種注冊(cè)方式:
- 靜態(tài)注冊(cè)鹃觉,在Manifest中注冊(cè):
<receiver android:name=".TestBroadcastReceiver">
<intent-filter>
<action android:name="com.example.hx.demo.broadcast"/>
</intent-filter>
</receiver>
2.動(dòng)態(tài)注冊(cè)专酗,創(chuàng)建一個(gè)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();
// 動(dòng)態(tài)注冊(cè)廣播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.hx.demo.broadcast");
registerReceiver(mTestBroadcastReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
// 反注冊(cè)
unregisterReceiver(mTestBroadcastReceiver);
}
}
-
在廣播接受端:
public class TestBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 不能做耗時(shí)操作,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.使用之前需要獲取聯(lián)網(wǎng)權(quán)限盗扇。
2.加載線上url或本地html
3.和JS進(jìn)行交互祷肯、歷史記錄、導(dǎo)航......
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);
// 加載網(wǎng)址
mWebView.loadUrl("http://www.baidu.com");
// 允許使用JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);
// 可重寫很多方法實(shí)現(xiàn)不同的功能
mWebView.setWebViewClient(new WebViewClient(){
});
}
@Override
public void onBackPressed() {
if (mWebView != null && mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Widget
Widget是BroadcastReceive的實(shí)現(xiàn)粱玲,由于Widget和App是相分離的躬柬,所以Widget是運(yùn)行再主屏幕進(jìn)程上的,所以和傳統(tǒng)的Activity的一些設(shè)置有很大不同抽减,有很多限制允青。
Android Widget詳解
Google Developers App Widgets