2013-12-03 97 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