Tải bản đầy đủ (.pptx) (31 trang)

android development introduction chương 22 vandroid services

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (389.91 KB, 31 trang )



22







Android Services
 ! "indenite"


# "$$%&'()&*$+(!+ 

,-./)"
!%(+%(+

***


*
Android Services

)Context.startService() ($(+
"+ !$!Context.stopService() 
stopSelf() 

!"


0$stopService() !$startService() !
onCreate
onStart
onDestroy
111


1
Service Life Cycle
2$ "$$
3$"!$
4$4$ 
1. void onCreate ()
2. void onStart (Intent intent)
3. void onDestroy ()
555


5
Service Life Cycle
#""!%(+$(+
2$ %(+ $(+
6 $!!$ (+
!"(+
777


7
Broadcast Receiver Lifecycle
38"9$! 

$
The system delivers a broadcast Intent to all interested broadcast receivers, which handle the Intent sequenally.
:::


:
Registering a Broadcast Receiver

;"""!Context.registerReceiver()

$<receiver> $AndroidManifest.xml.
<<<


<
Broadcast Receiver Lifecycle

void onReceive (Context context, Intent broadcastMsg)
= >" 8(+9?

# $"%&
* >8(+ 
onReceive
@@@


@
Services, BroadcastReceivers and the AdroidManifest
#""
= <service> $"

 9"ABroadcastReceiver <receiver> "$
9<intent-lter> $A
%
=B=B=B


=B
Services, BroadcastReceivers and the AdroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" /> package="cis493.demos" android:versionCode="1" android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="4"></uses-sdk>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyServiceDriver2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name="MyService2" />

<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name = "matos.action.GOSERVICE2" />
</intent-filter>
</receiver>

</application>
</manifest>

======


==
Types of Broadcasts
#!?"
1. Normal broadcasts(!Context.sendBroadcast+$""
A C
2. Ordered broadcasts(!Context.sendOrderedBroadcast+
  $(abortBroadcast()+
!/0"!android:priority
"DAE!$!$
===


=
Useful Methods – The Driver
$'"(!!!'"(#$"
"!
='"(
Intent intentMyService = new Intent(this, MyService3.class);
Service myService = startService(intentMyService);

AFA
IntentFilter mainFilter = new IntentFilter("matos.action.GOSERVICE3");
BroadcastReceiver receiver = new MyMainLocalReceiver();
registerReceiver(receiver, mainFilter);
*9
public void onReceive(Context localContext, Intent callerIntent)
=*=*=*



=*
Useful Methods – The Service
$'"(!!!'"(#
"!
=%9!A($"+
Intent myFilteredResponse = new Intent("matos.action.GOSERVICE3");
&(G$F+!(+
Object msg = some user data goes here;
myFilteredResponse.putExtra("myServiceData", msg);
*8A
sendBroadcast(myFilteredResponse);
=1=1=1


=1
Useful Methods – The Driver (again)
$'"(!!!'"(#$"$
"!
=intentMyService9%"$

stopService(new Intent(intentMyService) );
'F$"
unregisterReceiver(receiver);
=5=5=5


=5
Example 1. A very Simple Service

##)LogCat$9&%

H
package cis493.demos;
$E
%E
9E
3E
!I!E
!I!0%2E
!JE
public class ServiceDriver1 extends Ac7vity {
#I! )E
3 E
9 )$E
%E


=7=7=7


=7
Example 1. cont.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) findViewById(R.id.txtMsg);

intentMyService = new Intent(this, MyService1.class);

service = startService(intentMyService);

btnStopService = (Button) findViewById(R.id.btnStopService);
btnStopService.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
stopService((intentMyService) );
txtMsg.setText("After stoping Service: \n" + service.getClassName());
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), 1).show();
}
}//onClick
});
}//onCreate
}
=:=:=:


=:
Example 1. cont.
//non CPU intensive service running the main task in its main thread
package cis493.demos;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService1 extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;

}
@Override
public void onCreate() {
super.onCreate();
Log.i ("<<MyService1-onStart>>", "I am alive-1!");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i ("<<MyService1-onStart>>", "I did something very quickly");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i ("<<MyService1-onDestroy>>", "I am dead-1");
}
}//MyService1
=<=<=<


=<
Example 1. cont.
2
= )$($$+
 (% +
* )$'9$
1 '
=@=@=@



=@
Example 1. cont. Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" /> package="cis493.demos"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".ServiceDriver1"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService1"> </service>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
BBB


B
Example 1. cont. Layout
-KL)*+),) /)01
-2$
L)234(#)
$M!L)56)
$ML)56)
L7844444.

-,#
L)234%')
$M!L)56)
$ML)*#+%)
NL)*/)
$ML)+%)
$M$L)9:%;4.
-3
L)234)
$M!L)*9*%)
$ML)6)
L))
$ML)<(%)
$M$L)#++%;.
-2$.
===


=
Example 2. A More Realis7c Ac7vity-Service Interac7on
= #$
 #! "
* >!$ 
1 #AO0,8I9%,*
5 =&(A$+A($
'9+
7 $A





Example 2. Layout
-KL)*+),) /)01
-22$
L)234(#)
$M!L)56)
$ML)56)
L))
L)7844444)
.
-,#
L)234%')
$M!L)56)
$ML)*#+%)
NL)*#)
.
-,#.
-3
L)234)
$M!L)*9*%)
$ML)6)
L))
.
-3.
-22$.
***


*
Example 2. Manifest

-KL)*+),) /)01
-"L)7844444)
L)<>()
%L)*)
L)*++)1
-DIL)<)1?4.1
-L)24)8,)246)1
-$L)'"!()
L)246)1
-DA.
-L)'@AB)41
-$L)"C@DB EF&)41
-DA.
-$.

-L)'"()1
-.
-.
-".
111


1
// Application logic and its BroadcastReceiver in the same class
package cis493.demos;
import java.util.Date;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MyServiceDriver3 extends Activity {
TextView txtMsg;
Button btnStopService;
ComponentName service;
Intent intentMyService;
BroadcastReceiver receiver;
Example 2. Main Ac7vity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) findViewById(R.id.txtMsg);

intentMyService = new Intent(this, MyService3.class);
service = startService(intentMyService);

txtMsg.setText("MyService3 started - (see DDMS Log)");
btnStopService = (Button) findViewById(R.id.btnStopService);
btnStopService.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
stopService(new Intent(intentMyService) );

txtMsg.setText("After stoping Service: \n" +
service.getClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
});

555


5
Example 2. Main Ac7vity



×