2014年12月21日 星期日

Android開發零點起飛(第七章)筆記-Activity抓取Service內的值

Android開發零點起飛(第七章)筆記-Activity抓取Service內的值


 


 


//MainActivity.java


package com.example.jashsample;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/*
 00.目的新增一個有Service功能的範例
 01.新增CountService
 02.新增interface ICountService
 03.在設定檔新增<service android:name ="CountService" >的區段
 */
public class MainActivity extends Activity {
private ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countService = (ICountService) service;
Log.v( " CountService " , " on serivce connected, count is "
+ countService.getCount());
}

@Override
public void onServiceDisconnected(ComponentName name) {
countService = null ;
}

};
private ICountService countService;
private Button But1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
But1=new Button(this);
But1.setText("Get");
But1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Service的counter=" +countService.getCount(),Toast.LENGTH_LONG).show();
}
});
ll.addView(But1);
setContentView(ll);
this.bindService( new Intent("com.example.jashsample.CountService"),this.serviceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
this.unbindService(serviceConnection);//一定要在super.onDestroy();前面
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

//ICountService.java


package com.example.jashsample;

publicinterface ICountService {
publicabstractint getCount();
}

//CountService.java


package com.example.jashsample;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class CountService extends Service implements ICountService {

private boolean threadDisable;

private int count;

private ServiceBinder serviceBinder = new ServiceBinder();

public class ServiceBinder extends Binder implements ICountService{
@Override
public int getCount() {
return count;

}

}

@Override

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

@Override
public void onCreate() {
super .onCreate();
new Thread( new Runnable() {

@Override
public void run() {
while ( ! threadDisable) {
try {
Thread.sleep( 1000 );
} catch (InterruptedException e) {
}
count ++ ;
Log.v( " CountService " , " Count is " + count);
}
}
}).start();
}

@Override
public void onDestroy() {
super .onDestroy();
this .threadDisable = true ;
Log.v( " CountService " , " on destroy " );
}

/* (non-Javadoc)
     * @see com.easymorse.ICountService#getCount()
     */
public int getCount() {
return count;
}

}














 




沒有留言:

張貼留言