[ServiceStartArgumentsController.java]

   private OnClickListener mStart1Listener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(ServiceStartArgumentsController.this,
                    ServiceStartArguments.class)
                            .putExtra("name", "One"));
        }
    };

    private OnClickListener mStart2Listener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(ServiceStartArgumentsController.this,
                    ServiceStartArguments.class)
                            .putExtra("name", "Two"));
        }
    };

    private OnClickListener mStart3Listener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(ServiceStartArgumentsController.this,
                    ServiceStartArguments.class)
                            .putExtra("name", "Three")
                            .putExtra("redeliver", true));
        }
    };

    private OnClickListener mStartFailListener = new OnClickListener() {
        public void onClick(View v) {
            startService(new Intent(ServiceStartArgumentsController.this,
                    ServiceStartArguments.class)
                            .putExtra("name", "Failure")
                            .putExtra("fail", true));
        }
    };

    private OnClickListener mKillListener = new OnClickListener() {
        public void onClick(View v) {

            Process.killProcess(Process.myPid());
        }
    };



[ServiceStartArgument.java]

public class ServiceStartArguments extends Service {
    private NotificationManager mNM;
    private Intent mInvokeIntent;
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;

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

       
        @Override
        public void handleMessage(Message msg)
        {
            Bundle arguments = (Bundle)msg.obj;
       
            String txt = arguments.getString("name");
           
            Log.i("ServiceStartArguments", "Message: " + msg + ", "
                    + arguments.getString("name"));
       
            if ((msg.arg2&Service.START_FLAG_REDELIVERY) == 0) {
                txt = "New cmd #" + msg.arg1 + ": " + txt;
            } else {
                txt = "Re-delivered #" + msg.arg1 + ": " + txt;
            }
           
            showNotification(txt);
       
            // Normally we would do some work here...  for our sample, we will
            // just sleep for 5 seconds.

            long endTime = System.currentTimeMillis() + 5*1000;
            while (System.currentTimeMillis() < endTime) {
                synchronized (this) {
                    try {
                        wait(endTime - System.currentTimeMillis());
                    } catch (Exception e) {
                    }
                }
            }
       
            hideNotification();
           
            Log.i("ServiceStartArguments", "Done with #" + msg.arg1);
            stopSelf(msg.arg1);
        }

    };
   
    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        Toast.makeText(this, R.string.service_created,
                Toast.LENGTH_SHORT).show();
       
        mInvokeIntent = new Intent(this, ServiceStartArgumentsController.class);

        HandlerThread thread = new HandlerThread("ServiceStartArguments",
                Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();
       
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ServiceStartArguments",
                "Starting #" + startId + ": " + intent.getExtras());
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.arg2 = flags;
        msg.obj = intent.getExtras();
        mServiceHandler.sendMessage(msg);
        Log.i("ServiceStartArguments", "Sending: " + msg);
       
        if (intent.getBooleanExtra("fail", false)) {

            if ((flags&START_FLAG_RETRY) == 0) {

                Process.killProcess(Process.myPid());
            }
        }
       
        return intent.getBooleanExtra("redeliver", false)
                ? START_REDELIVER_INTENT : START_NOT_STICKY;

    }

    @Override
    public void onDestroy() {
        mServiceLooper.quit();

        hideNotification();

        Toast.makeText(ServiceStartArguments.this, R.string.service_destroyed,
                Toast.LENGTH_SHORT).show();

    }

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

    private void showNotification(String text) {
        Notification notification = new Notification(R.drawable.stat_sample, text,
                System.currentTimeMillis());

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, AlarmService.class), 0);

        notification.setLatestEventInfo(this, getText(R.string.service_start_arguments_label),
                       text, contentIntent);

        notification.flags |= Notification.FLAG_ONGOING_EVENT;
       
        mNM.notify(R.string.service_created, notification);
    }
   
    private void hideNotification() {
        mNM.cancel(R.string.service_created);

    }
}



[Manifest.xml]

<service android:name=".app.RemoteService" android:process=":remote">
            <intent-filter>
                <action android:name="com.example.android.apis.app.IRemoteService" />
                <action android:name="com.example.android.apis.app.ISecondary" />
             <action android:name="com.example.android.apis.app.REMOTE_SERVICE" />
            </intent-filter>
        </service>

'old > API_Demo' 카테고리의 다른 글

APP_VoiceRecognition  (0) 2010.03.25
APP_TextToSpeech  (0) 2010.03.25
APP_Service_RemoteServiceBinding  (0) 2010.03.25
APP_Service_RemoteServiceController  (0) 2010.03.25
APP_Service_LocalServiceController  (0) 2010.03.25
Posted by jazzlife
,