以前の「5秒後にノーティフィケーションを表示」の続きです。
今回はIntentServiceを使用してUIを変化させる方法です。ハンドラのインスタンスを持たせるクラスは、ApplicationクラスでなくてもSingletonであれば良いかと。こちらもやはりタスク一覧から消すとタイマーが停止します。
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aillice_android.example.alarmmanagersample"> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".MyBroadcast"/> <service android:name=".MyService"/> <service android:name=".AlarmService"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class MainActivity extends AppCompatActivity { MyApplication app; public static final SimpleDateFormat sdf = new SimpleDateFormat("kk:mm:ss"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Time time = new Time(5); Intent intent = new Intent(getApplicationContext(), AlarmService.class); intent.putExtra(Time.TAG, time); startService(intent); } @Override protected void onResume() { super.onResume(); app = (MyApplication) getApplication(); app.handler = new ViewHandler(); } @Override protected void onPause() { super.onPause(); app.handler = null; } class ViewHandler extends Handler{ @Override public void handleMessage(Message msg) { TextView textView = (TextView) findViewById(R.id.textView); textView.setText(sdf.format(Calendar.getInstance().getTime())); textView.append("\n" + msg.getData().getString("data")); } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.aillice_android.example.alarmmanagersample.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> |
MyApplication.java
1 2 3 4 5 6 7 8 |
public class MyApplication extends Application { public static final String TAG = MyApplication.class.getSimpleName(); public MainActivity.ViewHandler handler; } |
Time.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Time implements Serializable { public static final String TAG = Time.class.getSimpleName(); public int second = 0; public Calendar startCal, finishCal; public Time(int second) { this.second = second; this.startCal = Calendar.getInstance(); this.finishCal = Calendar.getInstance(); this.finishCal.add(Calendar.SECOND, second); } } |
AlarmService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class AlarmService extends IntentService { public static final String TAG = AlarmService.class.getSimpleName(); public AlarmService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Time t = (Time) intent.getSerializableExtra(Time.TAG); Timer timer = new Timer("timerA"); timer.schedule(new Task(t), 1000, 1000); } class Task extends TimerTask{ Time t; int sec = 0; public Task(Time t) { this.t = t; this.sec = t.second; } @Override public void run() { --sec; Log.d(TAG, "残りsec = " + sec); if (Calendar.getInstance().getTimeInMillis() >= t.finishCal.getTimeInMillis() && Calendar.getInstance().get(Calendar.SECOND) == t.finishCal.get(Calendar.SECOND)){ Intent intent = new Intent(getApplicationContext(), MyService.class); intent.putExtra(Time.TAG, t); startService(intent); } if (Calendar.getInstance().getTimeInMillis() >= t.finishCal.getTimeInMillis() + 1000) cancel(); } } } |
MyService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class MyService extends IntentService { public static final String TAG = MyService.class.getSimpleName(); public MyService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "hogehoge. "); MyApplication app = (MyApplication) getApplication(); if (app.handler != null){ Bundle arg = new Bundle(); arg.putString("data", "hoge--hoge"); Message message = new Message(); message.setData(arg); Messenger messenger = new Messenger(app.handler); try { messenger.send(message); } catch (RemoteException e) { e.printStackTrace(); } } } } |
Prev
Next