OSDN Git Service

スケジューリングのロジック作成。未テスト。テストクラスを作成すること
[train-delayed/source.git] / workspace / TrainDelayed / src / com / td / service / TrainDelayedServiceImpl.java
index 11e4d28..1dea82d 100644 (file)
@@ -9,6 +9,8 @@ import twitter4j.AsyncTwitter;
 import twitter4j.AsyncTwitterFactory;\r
 import twitter4j.Query;\r
 import android.app.AlarmManager;\r
+import android.app.Notification;\r
+import android.app.NotificationManager;\r
 import android.app.PendingIntent;\r
 import android.app.Service;\r
 import android.content.Context;\r
@@ -16,6 +18,8 @@ import android.content.Intent;
 import android.os.Binder;\r
 import android.os.IBinder;\r
 \r
+import com.td.R;\r
+import com.td.TrainDelayedActivity;\r
 import com.td.TrainTwitterAdapter;\r
 import com.td.db.Train;\r
 import com.td.db.TrainDao;\r
@@ -26,14 +30,14 @@ public class TrainDelayedServiceImpl extends Service {
        public static final String ACTION = TrainDelayedServiceImpl.class\r
                        .getSimpleName();\r
        private final IBinder binder = new Binder();\r
-       \r
-//     private boolean isCanceled = false;\r
-//     \r
-//     public class TrainDelayedBinder extends Binder {\r
-//             public TrainDelayedServiceImpl getService() {\r
-//                     return TrainDelayedServiceImpl.this;\r
-//             }\r
-//     }\r
+\r
+       // private boolean isCanceled = false;\r
+       //\r
+       // public class TrainDelayedBinder extends Binder {\r
+       // public TrainDelayedServiceImpl getService() {\r
+       // return TrainDelayedServiceImpl.this;\r
+       // }\r
+       // }\r
 \r
        // TODO\r
        private AsyncTwitterFactory factory = new AsyncTwitterFactory();\r
@@ -41,7 +45,7 @@ public class TrainDelayedServiceImpl extends Service {
        private TrainTwitterAdapter listener = new TrainTwitterAdapter(this);\r
 \r
        private Timer timer = null;\r
-       private int id = 0;\r
+       private static int id = 0;\r
 \r
        @Override\r
        public IBinder onBind(Intent intent) {\r
@@ -113,7 +117,7 @@ public class TrainDelayedServiceImpl extends Service {
        }\r
 \r
        // TODO\r
-       //private PendingIntent alarmSender;\r
+       // private PendingIntent alarmSender;\r
 \r
        public void schedule2(long delay) {\r
                try {\r
@@ -135,18 +139,47 @@ public class TrainDelayedServiceImpl extends Service {
                                query.setQuery(text);\r
                                twitter.search(query);\r
                        }\r
-\r
+                       // TODO\r
+                       //notificate("id = " + id);\r
+                       \r
                        id++;\r
                } catch (Exception e) {\r
                        Debug.d(this, null, e);\r
                }\r
        }\r
 \r
-//     public void scheduleStop() {\r
-//             if (timer != null) {\r
-//                     timer.cancel();\r
-//             }\r
-//     }\r
+       private void notificate(String msg) {\r
+               try {\r
+                       String ns = Context.NOTIFICATION_SERVICE;\r
+                       NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);\r
+\r
+                       int icon = R.drawable.icon;\r
+                       CharSequence tickerText = "\93d\8eÔ\82ª\92x\82ê\82Ä\82é?";\r
+                       long when = System.currentTimeMillis();\r
+                       Notification notification = new Notification(icon, tickerText, when);\r
+\r
+                       Context context = getApplicationContext();\r
+                       CharSequence contentTitle = "My notification";\r
+                       CharSequence contentText = msg;\r
+                       Intent notificationIntent = new Intent(this,\r
+                                       TrainDelayedActivity.class);\r
+                       PendingIntent contentIntent = PendingIntent.getActivity(this, 0,\r
+                                       notificationIntent, 0);\r
+                       notification.setLatestEventInfo(context, contentTitle, contentText,\r
+                                       contentIntent);\r
+\r
+                       int HELLO_ID = 1;\r
+                       mNotificationManager.notify(HELLO_ID, notification);\r
+               } catch (Exception e) {\r
+                       Debug.d(this, null, e);\r
+               }\r
+       }\r
+\r
+       // public void scheduleStop() {\r
+       // if (timer != null) {\r
+       // timer.cancel();\r
+       // }\r
+       // }\r
 \r
        @Override\r
        public void onCreate() {\r
@@ -167,7 +200,7 @@ public class TrainDelayedServiceImpl extends Service {
                                                Debug.d(this, null, e);\r
                                        }\r
                                }\r
-                                       \r
+\r
                                // \8e\9f\89ñ\8bN\93®\93o\98^\r
                                long now = System.currentTimeMillis();\r
                                PendingIntent alarmSender = PendingIntent.getService(\r
@@ -175,7 +208,8 @@ public class TrainDelayedServiceImpl extends Service {
                                                                TrainDelayedServiceImpl.this,\r
                                                                TrainDelayedServiceImpl.class), 0);\r
                                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);\r
-                               alarm.set(AlarmManager.RTC, now + DateUtil.LEN * 60 * 1000, alarmSender);\r
+                               alarm.set(AlarmManager.RTC, now + DateUtil.LEN * 60 * 1000,\r
+                                               alarmSender);\r
 \r
                                // \83T\81[\83r\83X\8fI\97¹\r
                                TrainDelayedServiceImpl.this.stopSelf();\r
@@ -200,8 +234,8 @@ public class TrainDelayedServiceImpl extends Service {
        @Override\r
        public void onDestroy() {\r
                Debug.d(this, Debug.ON_DESTROY);\r
-               //TODO\r
-               //scheduleStop();\r
+               // TODO\r
+               // scheduleStop();\r
 \r
                super.onDestroy();\r
        }\r