使用TaskTimer发送通知

时间:2013-12-03 18:41:14

标签: android notifications

我想在特定日期或时间发送通知。我试图实现这里解释的例子:http://android-er.blogspot.com/2011/04/simple-example-to-send-notification.html。但我得到强制关闭错误和logcat给出非法状态异常。在onCreate之前,活动无法使用系统服务。

这是我的logcat:

   12-03 23:19:59.712: E/AndroidRuntime(4725): FATAL EXCEPTION: Timer-0
12-03 23:19:59.712: E/AndroidRuntime(4725): java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-03 23:19:59.712: E/AndroidRuntime(4725):     at android.app.Activity.getSystemService(Activity.java:3526)
12-03 23:19:59.712: E/AndroidRuntime(4725):     at com.smsalarmmanager.MainActivity.triggernotificaion(MainActivity.java:79)
12-03 23:19:59.712: E/AndroidRuntime(4725):     at com.smsalarmmanager.ScheduleClass.run(ScheduleClass.java:15)
12-03 23:19:59.712: E/AndroidRuntime(4725):     at java.util.Timer$TimerImpl.run(Timer.java:289)
12-03 23:24:59.743: I/Process(4725): Sending signal. PID: 4725 SIG: 9

MainActivity.java:

public class MainActivity extends Activity {

    private static final int MY_NOTIFICATION_ID=1;
    private NotificationManager notificationManager;
    private Notification myNotification;

    private final String myBlog = "http://android-er.blogspot.com/";

    Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


          Timer timer = new Timer();

          SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm a");
        String dateString  = "Tue, Dec 3, 2013 11:15 PM";

                try {

                Date    when = sdf.parse(dateString);


                // scheduling the task

                  timer.schedule(new ScheduleClass(), when);


                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }      





    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public void triggernotificaion()
    {


        notificationManager =
                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
               myNotification = new Notification(R.drawable.ic_launcher,
                 "Notification!",
                 System.currentTimeMillis());
               Context context = getApplicationContext();
               String notificationTitle = "Exercise of Notification!";
               String notificationText = "Text For Sample Notification";
               Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
               PendingIntent pendingIntent
                 = PendingIntent.getActivity(MainActivity.this,
                   0, myIntent,
                   Intent.FLAG_ACTIVITY_NEW_TASK);
               myNotification.defaults |= Notification.DEFAULT_SOUND;
               myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
               myNotification.setLatestEventInfo(context,
                  notificationTitle,
                  notificationText,
                  pendingIntent);
               notificationManager.notify(MY_NOTIFICATION_ID, myNotification);


    }
}

ScheduleClass.java:

public class ScheduleClass extends TimerTask {

    MainActivity ma = new MainActivity();
    @Override
    public void run() {
        // TODO Auto-generated method stub

        ma.triggernotificaion();

    }

}

请问有谁可以解释我哪里出错了?我尝试在使用相同的tasktimer类之前发送短信,并且它工作正常。但无法发送通知。

请帮忙。

1 个答案:

答案 0 :(得分:1)

这是因为您调用了MainActivity的方法,但您的活动尚未创建,只是实例化。

您应该将triggernotificaion方法设为静态(您可能需要Context参数)。

例如,您的ScheduleClass.java

public class ScheduleClass extends TimerTask {
    private static final int MY_NOTIFICATION_ID=1;
    private Context context;

    private static final String myBlog = "http://android-er.blogspot.com/";

    public ScheduleClass(Context context){
        super();
        this.context = context;
    }


    @Override
    public void run() {
        // TODO Auto-generated method stub

        ScheduleClass.triggernotificaion(context);

    }

    public static void triggernotificaion(Context context, String myBlog){
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification myNotification = new Notification(R.drawable.ic_launcher, "Notification!", System.currentTimeMillis());
        String notificationTitle = "Exercise of Notification!";
        String notificationText = "Text For Sample Notification";
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    }

}

然后,您只需在启用ScheduleClass

时提供上下文