準備
1. こちらを参照して、Test1Activity2 を追加します。
デザイン
1. main.xml にボタン (button1) を配置します。
サンプルコード (Java) - main アクティビティ
// import の追加
import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Button;
import android.content.*;
import android.view.View;
import android.view.View.OnClickListener;
// コード
public class Test20Activity extends Activity {
private ITest20 MyService;
private ServiceConnection mServiceConn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = new Intent(Test20Activity.this, Test20Service.class);
Button btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mServiceConn = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
MyService = ITest20.Stub.asInterface(service);
ServiceStart(5);
}
public void onServiceDisconnected(ComponentName name) {
MyService = null;
}
};
bindService(intent, mServiceConn, BIND_AUTO_CREATE);
}
});
Button btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (MyService != null) {
MyService = null;
unbindService(mServiceConn);
}
stopService(intent);
}
});
Button btnChange = (Button) findViewById(R.id.btnChange);
btnChange.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ServiceStart(1);
}
});
}
public void ServiceStart(int v1) {
try {
MyService.Change(v1);
MyService.Start();
} catch (RemoteException e) {
}
}
public void onStart() {
super.onStart();
}
public void onStop() {
super.onStop();
if (MyService != null){
unbindService(mServiceConn);
}
}
}
サンプルコード (Java) - Test1Activity2 アクティビティ
// import の追加
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
// コード
public class Test20Service extends Service {
private long Interval = 1000;
private boolean Run = false;
private NotificationManager NotifMan;
private ITest20.Stub Svcstub = new MyStub();
private class MyStub extends ITest20.Stub {
public void Start() throws RemoteException {
if (!Handler.hasMessages(1)) {
Handler.sendEmptyMessage(1);
}
}
public void Stop() throws RemoteException {
NotifMan.cancel(0);
Handler.removeMessages(1);
}
public void Change(int sec) throws RemoteException {
Interval = sec * 1000;
}
}
private Handler Handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
cal.setTime(new Date());
String sWork = String.valueOf(cal.get(Calendar.HOUR)) + ":"
+ String.valueOf(cal.get(Calendar.MINUTE)) + ":"
+ String.valueOf(cal.get(Calendar.SECOND));
showNotification(sWork);
if(Run) {
Handler.sendEmptyMessageDelayed(1, Interval);
}
}
};
private void showNotification(String sTime) {
Notification notif = new Notification(R.drawable.ic_launcher, sTime, System.currentTimeMillis());
Intent intent = new Intent(this, Test20Activity.class);
PendingIntent pit = PendingIntent.getActivity(Test20Service.this, 0, intent, 0);
notif.setLatestEventInfo(Test20Service.this, "現在の時間", sTime, pit);
NotifMan.notify(0, notif);
}
@Override
public IBinder onBind(Intent intent) {
return Svcstub;
}
@Override
public void onCreate() {
super.onCreate();
Run = true;
NotifMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onDestroy(){
super.onDestroy();
Run = false;
NotifMan.cancel(0);
Handler.removeMessages(1);
}
}
解説
現在時刻を表示するサービスを起動しています。時間の更新間隔はデフォルトでは 5 秒ですが、Change ボタンをタップすること、1 秒に変更しています。

