Ana səhifə

Context startService


Yüklə 34.5 Kb.
tarix26.06.2016
ölçüsü34.5 Kb.
http://marakana.com/forums/android/examples/60.html
To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).

Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to startService() which envokes the service onCreate() method and onStart() beginning running the service.



context.startService() | ->onCreate() - >onStartCommand() [service running]

Calling the applications stopService() method to stop the service.



context.stopService() | ->onDestroy() [service stops]

Something that we didn't use in this example is bindService() which just calls the services onCreate() method but does not call the onStartCommand(). onBindService() is used to create persistance connection to the service.



context.onBindService() | ->onCreate() [service created]

This Services Demo is simple as it plays a audio file and by listening to click events of the buttons invokes the MyService service.


ServicesDemo.java
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;

@Override


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.buttonStart);


buttonStop = (Button) findViewById(R.id.buttonStop);

buttonStart.setOnClickListener(this);


buttonStop.setOnClickListener(this);
}

public void onClick(View src) {


switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
}
}
MyService.java
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;

@Override


public IBinder onBind(Intent intent) {
return null;
}

@Override


public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");

player = MediaPlayer.create(this, R.raw.braincandy);


player.setLooping(false); // Set looping
}

@Override


public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}

@Override


public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
AndroidManifest.xml


package="com.example" android:versionCode="1" android:versionName="1.0">











main.xml


android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Services Demo" android:gravity="center" android:textSize="20sp" android:padding="20dp"/>





Notificações
public void notifica(int id) {

if(!notificar) return; // Notifies only once for each trip

notificar = false;

NotificationManager notificationManager = (NotificationManager)

ctx.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = createNotification();

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, null, 0);

notification.setLatestEventInfo(ctx,

ctx.getResources().getString(R.string.texto1),

ctx.getResources().getString(R.string.texto2),

pendingIntent);

notificationManager.notify(NOTIFICATION_ID, notification);

}

private Notification createNotification() {



Notification notification = new Notification();

notification.icon = R.drawable.ic_menu_notifications;

notification.when = System.currentTimeMillis();

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.flags |= Notification.FLAG_SHOW_LIGHTS;

//notification.defaults |= Notification.DEFAULT_VIBRATE;

notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.defaults |= Notification.DEFAULT_SOUND;

notification.ledARGB = Color.WHITE;

notification.ledOnMS = 1500;

notification.ledOffMS = 1500;

return notification;



}


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©atelim.com 2016
rəhbərliyinə müraciət