2012-11-09 78 views
1

我使用線程創建了一個應用程序。 在線程中,我有一個委託人發送通知,但它不工作。 我錯在哪裏?線程通知錯誤

代碼:

NotificationManager notificationManager; 
    Notification myNotification; 
    private final String myBlog = "http://www.example.com/"; 
    private static final int MY_NOTIFICATION_ID=1; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.dashboard); 

userFunctions = new UserFunctions(); 

    inputUsername = (TextView) findViewById(R.id.loginUsername); 
     btnLogout = (Button) findViewById(R.id.btnLogout); 
     inputThread = (TextView) findViewById(R.id.thread_modified_text); 
     SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE); 
     username = userDetails.getString("username", ""); 
     inputUsername.setText(username);  

currentThread = new Thread(this); 
      currentThread.start(); 

} 

    @Override 
    public void run() {  
     try { 
        while(username != null) 
        { 
      Thread.sleep(5000); 
      threadHandler.sendEmptyMessage(0);   
        }          
     } catch (InterruptedException e) { 
      //  
     } 
    } 

private Handler threadHandler = new Handler() 
{ 

public void handleMessage(android.os.Message msg) { 

    myNotification = new Notification(R.drawable.stat_notify_chat, 
        "A new notificatin!", 
        System.currentTimeMillis());  
    Context context = getApplicationContext();  
    String notificationTitle = "Notification";  
    String notificationText = "XXXX";  
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));  
    PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),  
            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);  

} 
} 
+0

這是行不通的。這是什麼意思。你有什麼錯誤嗎?? –

+0

我的應用程序崩潰..「不幸已停止等」 – user1811710

+0

因此,你會介意張貼你的日誌貓stacktrace。 –

回答

0

我想你應該寫Looper.prepare()中的run()您thread.This的方法爲我工作.. 希望這有助於。