0

这是我的代码:

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 设备上的通知中显示进度条?

谢谢!

4

3 回答 3

2

没关系..

我将代码更改为:

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我对此无能为力..我现在能想到的唯一方法是带有进度条的自定义布局...

于 2013-08-07T03:59:27.170 回答
1

您是否尝试过将 v4.jar 库导入您的项目?它支持在低 api 上运行的新 api 版本的功能(例如 Android 2.2)。

干杯,

于 2013-08-07T03:54:20.890 回答
0

我重写了您的代码以启用多个通知进度。还更新为适用于 android Oreo+

class MainActivity : AppCompatActivity(){
    private var mNotifyManager: NotificationManagerCompat? = null
    private var mBuilder: NotificationCompat.Builder? = null
    private var notificationId = 0
    val CHANNEL_ID ="download_progress_notification"


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mNotifyManager = NotificationManagerCompat.from(this)
        mBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
    }
    private fun noti(notficationId : Int) {
        mBuilder?.setContentTitle("Picture Download")
                ?.setContentText("Download in progress")
                ?.setSmallIcon(R.mipmap.ic_launcher)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // you must create a notification channel for API 26 and Above
            val name = "my channel2"
            val description = "channel description2"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description)
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(channel)
        }

        // Start a lengthy operation in a background thread
        Thread(object : Runnable{
            override fun run() {
                var incr: Int
                // Do the "lengthy" operation 20 times
                incr = 0
                while (incr <= 100) {
                    // 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(notficationId, mBuilder!!.build())
                    // Sleeps the thread, simulating an operation
                    // that takes time
                    try {
                        // Sleep for 5 seconds
                        Thread.sleep((1 * 1000).toLong())
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }

                    incr += 5
                }
                // When the loop is finished, updates the notification
                mBuilder?.setContentText("Download complete")
                        // Removes the progress bar
                        ?.setProgress(0, 0, false)
                mNotifyManager?.notify(notficationId, mBuilder!!.build())
            }
        }).start()
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            R.id.menu_option -> {
                noti(notificationId++)
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
}
于 2018-09-18T17:59:36.553 回答