0

這裏顯示的進步是我的代碼:Android的通知中

public class MainActivity extends Activity { 

private NotificationManager mNotifyManager; 
private NotificationCompat.Builder mBuilder; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mBuilder = new NotificationCompat.Builder(this); 
} 


public void click(View view) { 
    noti(); 
} 


private void noti() { 
    mBuilder.setContentTitle("Picture Download") 
     .setContentText("Download in progress") 
     .setSmallIcon(R.drawable.ic_launcher); 
    // Start a lengthy operation in a background thread 
    new Thread(
     new Runnable() { 
      @Override 
      public void run() { 
       int incr; 
       // Do the "lengthy" operation 20 times 
       for (incr = 0; incr <= 100; incr+=5) { 
         // Sets the progress indicator to a max value, the 
         // current completion percentage, and "determinate" 
         // state 
         mBuilder.setProgress(100, incr, false); 
         // Displays the progress bar for the first time. 
         mNotifyManager.notify(0, mBuilder.build()); 
          // Sleeps the thread, simulating an operation 
          // that takes time 
          try { 
           // Sleep for 5 seconds 
           Thread.sleep(1*1000); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 
       } 
       // When the loop is finished, updates the notification 
       mBuilder.setContentText("Download complete") 
       // Removes the progress bar 
         .setProgress(0,0,false); 
       mNotifyManager.notify(0, mBuilder.build()); 
      } 
     } 
    // Starts the thread by calling the run() method in its Runnable 
    ).start(); 
} 

}

它正常工作在Android 4.2.2設備,但是當我試圖在Android 2.2的設備上運行時,通知沒有出現,我得到了以下日誌:

RuntimeException in notify - 
java.lang.Throwable: stack dump 
at android.app.NotificationManager.notify(NotificationManager.java:118) 
at android.app.NotificationManager.notify(NotificationManager.java:92) 
at com.gyh.notitest.MainActivity$1.run(MainActivity.java:49) 
at java.lang.Thread.run(Thread.java:1019) 

任何人都可以幫助我嗎?如何在沒有自定義佈局的android 2.2設備上的通知中顯示進度條?

謝謝!

回答

1

您是否嘗試將v4.jar庫導入到您的項目中?它支持以低api運行的新api版本(例如Android 2.2)的功能。

乾杯,

+0

是的,我使用的Builder是來自v4.jar^_^ – John

+0

嗯,這裏不太確定。對不起,我無法幫助你解決問題。無論如何,嘗試與其他Android版本,如2.3,4.0看看會發生什麼? –

1

沒關係..

我我的代碼更改爲:

public class MainActivity extends Activity { 

private NotificationManager mNotifyManager; 
private NotificationCompat.Builder mBuilder; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent resultIntent = new Intent(this, MainActivity.class); 
    PendingIntent resultPendingIntent = 
     PendingIntent.getActivity(
     this, 
     0, 
     resultIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT 
    ); 


    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mBuilder = new NotificationCompat.Builder(
      getApplicationContext()).setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("My notification") 
      .setContentText("Hello World!") 
      .setContentIntent(resultPendingIntent); 
} 


public void click(View view) { 
    noti(); 
} 


private void noti() { 
    // Start a lengthy operation in a background thread 
    new Thread(
     new Runnable() { 
      @Override 
      public void run() { 
       int incr; 
       // Do the "lengthy" operation 20 times 
       for (incr = 0; incr <= 100; incr+=5) { 
         // Sets the progress indicator to a max value, the 
         // current completion percentage, and "determinate" 
         // state 
         mBuilder.setProgress(100, incr, false); 
         // Displays the progress bar for the first time. 
         mNotifyManager.notify(0, mBuilder.build()); 
          // Sleeps the thread, simulating an operation 
          // that takes time 
          try { 
           // Sleep for 5 seconds 
           Thread.sleep(1*1000); 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 
       } 
       // When the loop is finished, updates the notification 
       mBuilder.setContentText("Download complete") 
       // Removes the progress bar 
         .setProgress(0,0,false); 
       mNotifyManager.notify(0, mBuilder.build()); 
      } 
     } 
    // Starts the thread by calling the run() method in its Runnable 
    ).start(); 
} 

} 

然後纔出現了通知,但T_T有沒有進展是什麼我可以做些什麼。 。現在我唯一能想到的方法是用進度條自定義佈局...