2013-12-03 33 views
-1

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

这里是我的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(); 

    } 

} 

任何人能请解释一下我要去哪里错了?我尝试使用相同的任务时间类之前发送短信,它工作正常。但无法发送通知。

请帮忙。

回答

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); 
    } 

} 

然后,你就必须给上下文instiating ScheduleClass