수요일, 10월 27, 2010

[Android] My Tip; RemoteService Example

RemoteService Example

Reference Source:
http://developer.android.com/guide/developing/tools/aidl.html
http://developer.android.com/reference/android/app/Service.html
http://www.androidpub.com/102370



// -----------------------------------------------
// more Activity
// AndroidManifest.xml
// -----------------------------------------------
<activity android:name=".MainActivity"
android:label="MainActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".anotherActivity"
android:label="anotherActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

// -----------------------------------------------
// RemoteService
// AndroidManifest.xml
// -----------------------------------------------
<service android:enabled="true" android:name=".testActivity">
</service>


// -----------------------------------------------
// RemoteService
// AndroidManifest.xml
// -----------------------------------------------


// -----------------------------------------------
// RemoteService
// Service.java
// -----------------------------------------------
package com.test.testActivity;

// Service
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteCallbackList;

import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

/*
@Activity A (test)
startService( new Intent(test.testActivity, testService.class).putExtra("tag", "data") );

String[] strList = {
{"strList1"},
{"strList2"}
};
startService( new Intent(test.testActivity, testService.class).putExtra("tag2", strlist) );

@Service B (testService)
Bundle extras = getIntent().getExtras();
String[] strList = extras.getStringArray( "strlist" );
*/
public class testService extends Service {
private String TAG = "testService";

// Service
private NotificationManager mNM;
private Intent m_InvokeIntent;
private volatile Looper m_ServiceLooper;
private volatile ServiceHandler m_ServiceHandler;
public String m_Str = null;

final RemoteCallbackList m_Callbacks
= new RemoteCallbackList();

private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}

@Override
public void handleMessage(Message msg) {
Bundle extras = (Bundle)msg.obj;
String strTagValue = null;

/*
{
// Broadcast
strTagValue = extras.getString( "command" );
if( strTagValue != null ) {
if( strTagValue.equals("pause") ) {
stopSelf( msg.arg1 );
return;
}
else if( strTagValue.equals("stop") ) {
stopSelf( msg.arg1 );
return;
}
}
}
*/

strTagValue = null;
strTagValue = extras.getString( "tag" );
Log.d( TAG, "Message: " + msg + ", tagValue = " + strTagValue );

if( strTagValue != null ) {
if( strTagValue.equals("data") ) {
}
else if( strTagValue.equals("strlist") ) {
m_strPlayList = extras.getStringArray( "strlist" );
}
else {
Log.d( TAG, "QUIT" );
stopSelf( msg.arg1 );
return;
}
}
else {
stopSelf( msg.arg1 );
return;
}
}
};
  
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService( NOTIFICATION_SERVICE );
HandlerThread thread = new HandlerThread( "MediaServiceStart" );
thread.start();

m_ServiceLooper = thread.getLooper();
m_ServiceHandler = new ServiceHandler( m_ServiceLooper );
}

@Override
public void onStart(Intent intent, int startId) {
Log.d( TAG, "ServiceStartArguments: Starting #" + startId + ": " + intent.getExtras() );
{
Message msg = m_ServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent.getExtras();
m_ServiceHandler.sendMessage( msg );
Log.d( TAG, "ServiceStartArguments: Sending: " + msg );

// Test: Broadcast
/*
final int n = m_Callbacks.beginBroadcast();
Log.d( TAG, "n = " + n );
for( int i = 0; i < n; i++ )
Log.d( TAG, "item = " + m_Callbacks.getBroadcastItem(i) );
m_Callbacks.finishBroadcast();
*/
}
}

@Override
public void onDestroy() {
m_ServiceLooper.quit();
}

@Override
public IBinder onBind(Intent intent) {
return m_Binder;
}

// The IRemoteInterface is defined through IDL
private final IRemoteService.Stub m_Binder = new IRemoteService.Stub() {
public void registerCallback(IRemoteServiceCallback cb) {
if (cb != null) m_Callbacks.register(cb);
}

public void unregisterCallback(IRemoteServiceCallback cb) {
if (cb != null) m_Callbacks.unregister(cb);
}

public String getInfo() {
String str = "i'm service";
return str;
}
public String setInfo(String str) {
Log.d( TAG, "RECEIVED From Activity: " + str );
return str;
}
};
}


// -----------------------------------------------
// testActivity.java
// -----------------------------------------------
// Service
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.IBinder;
import android.os.RemoteException;
public class testActivity extends Activity implements {
// Service
private IRemoteService m_Service = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//setContentView(R.layout.main);
//Intent(testActivity.this, testService.class)
//Intent intt = new Intent( testActivity.this, Activity2.class );
//startActivity( intt );

//bindService( new Intent(testService.class.getName()), m_Connection, Context.BIND_AUTO_CREATE );
bindService( new Intent(testActivity.this, testService.class), m_Connection, Context.BIND_AUTO_CREATE );
startService( new Intent(testActivity.this, testService.class).putExtra("tag", "quit") );

/*
if( m_Service != null ) {
try {
m_Service.unregisterCallback( m_Callback );
} catch (RemoteException e) {
}
}

// Detach our existing connection.
unbindService( m_Connection );
stopService( new Intent(testActivity.this, testService.class) );
*/
}

@Override
protected void onDestroy() {
if( m_Service != null ) {
try {
m_Service.unregisterCallback( m_Callback );
} catch (RemoteException e) {
}
}

if( m_Connection != null ) {
// Detach our existing connection.
unbindService( m_Connection );
}
stopService( new Intent(testActivity.this, testService.class) );

super.onDestroy();
}

// Service
private ServiceConnection m_Connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
m_Service = IRemoteService.Stub.asInterface(service);
try {
m_Service.registerCallback(m_Callback);
Log.d( TAG, "RECEIVED From Service: " + m_Service.getInfo() );
m_Service.setInfo( "i'm activity" );
} catch (RemoteException e) {
}
Toast.makeText( testActivity.this, "Service Connected", Toast.LENGTH_SHORT ).show();
}

public void onServiceDisconnected(ComponentName className) {
m_Service = null;
Toast.makeText( testActivity.this, "Service Disconnected", Toast.LENGTH_SHORT ).show();
}
};

// This implementation is used to receive callbacks from the remote service.
private IRemoteServiceCallback m_Callback = new IRemoteServiceCallback.Stub() {
public void valueChanged(int value) {
//mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
}

// -----------------------------------------------
// IRemoteService.aidl (default + method)
// Source (v1.6): C:/android-sdk-windows/platforms/android-4/samples/ApiDemos/src/com/example/android/apis/app
// -----------------------------------------------
package com.test.testActivity;
import com.test.testActivity.IRemoteServiceCallback;
interface IRemoteService {
void registerCallback(IRemoteServiceCallback cb);
void unregisterCallback(IRemoteServiceCallback cb);
// ADD
String getInfo();
String setInfo(String str);
}

// -----------------------------------------------
// IRemoteServiceCallback.aidl (default)
// Source (v1.6): C:/android-sdk-windows/platforms/android-4/samples/ApiDemos/src/com/example/android/apis/app
// -----------------------------------------------
default



-----
Cheers,
June

댓글 없음:

댓글 쓰기