Bu uygulamada ListActivity içinden tıklanan değeri Service göndermek ve Service deki değeri de başka bir activitye göndermeye çalıştım. Yani MyDataPassing.java activitiysindeki listede tıklanan değeri PlayService.java ya gönderip orada Log.d() ile DDMS ye log atıyor ve giden isim değerini MyLastActivity.java içine gönderip toast mesajı olarak ekrana yazıyor.
Start Activity from Service and Start Service from Activity ve Passing data to another Activity Android ListView Example samplelerine bakarsanız iyi olur.
Service içinden DDMS e atılan loglar.

MyDataPassing.java
package com.data.passing;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MyDataPassing extends ListActivity{
private ArrayList<String> list=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list.add("Ethem");
list.add("Kenan");
list.add("Burhan");
list.add("Leyli");
list.add("Lola");
//Adapter için liste içinde default olarak tanımlı olan simple_list_item_checked kullandım.this.setListAdapter(adapter); ile adaptırı ekledik.
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list);
this.setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Listdeki değerler click edildiğinde o positiondaki itemi alıyor: this.getListAdapter().getItem(position)
//Aşağıdaki yorum satirlarindanki gibi clik edilen itema erişebiliyoruz.
// Object object=this.getListAdapter().getItem(position);
// String clicked=object.toString();
//int position click edilen degerin positionu ve list.get(position) ile o değere erişebiliyoruz. Diğer erişme yolu da yukar getItem() metodudur.
Intent intent_notify=new Intent(this, PlayService.class);
Bundle bundle=new Bundle();
bundle.putString("isim", list.get(position));
intent_notify.putExtra("bundle", bundle);
startService(intent_notify);
}
}
PlayService.java
package com.data.passing;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class PlayService extends Service {
private final IBinder mBinder = new LocalBinder();
private String name;
//Return the communication channel to the service.
@Override
public IBinder onBind(Intent intent) {
Log.d("mesaj", "onBind");
return mBinder;
}
//Called by the system when the service is first created.
@Override
public void onCreate() {
super.onCreate();
Log.d("mesaj", "Merhaba");
}
//Called by the system to notify a Service that it is no longer used and is being removed.
@Override
public void onDestroy() {
super.onDestroy();
Log.d("mesaj", "destroy");
}
//This method is deprecated. Implement onStartCommand(Intent, int, int) instead.
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("mesaj", "Merhaba start");
//MyDataPassing.java Activity nin icinden gonderilen degerlere erisiyoruz.
Bundle bundle = intent.getBundleExtra("bundle");
if(bundle!=null){
name=bundle.getString("isim");
}
Log.d(">>>", name);
//PlayService.java servicenin icinden alinan datayi MyLastActivity.java activitysine gonderiyorum.
Intent yeniActivityBas = new Intent(getBaseContext(),MyLastActivity.class);
yeniActivityBas.putExtra("sName", name);
yeniActivityBas.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(yeniActivityBas);
}
//Called when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent).
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.d("mesaj", "Rebind");
}
//onStartCommand(Intent intent, int flags, int startId)
//Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//Called when all clients have disconnected from a particular interface published by the service.
@Override
public boolean onUnbind(Intent intent) {
Log.d("mesaj", "onUnbind");
return super.onUnbind(intent);
}
//Local service için nested class tanimlama
class LocalBinder extends Binder {
PlayService getService() {
return PlayService.this;
}
}
}
MyLastActivity.java
package com.data.passing;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class MyLastActivity extends Activity{
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//PlayService.java icinden yani serviceden gonderilen degeri alip ekrana ayziyorum.
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
name=bundle.getString("sName");
}
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml içinde Service ve Activity tanımlamasını unutmayın benim gibi
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.data.passing" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyDataPassing" 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="PlayService"></service> <activity android:name="MyLastActivity"></activity> </application> </manifest>


Write a Comment
Let me know what you think?