我正在使用eclipse IDE在android中创建一个应用程序,我想在我的android中实现一个通知我想在按钮点击示例上发出通知我想通知已保存的数据..我发现这个教程here我复制了代码并将其粘贴到我的setOnClickListener代码中..但我得到错误..我不知道如何开始这样做,因为在教程中他们只是给你代码片段。这是我的代码
更新问题
通知和推送通知之间有什么区别,哪个更好用..
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// 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(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(ID, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
}
});
答案 0 :(得分:3)
你应该改变这个
mBuilder = new NotificationCompat.Builder(this);
到
mBuilder = new NotificationCompat.Builder(youractivity.this);
您需要传递当前Context
来创建mBuilder
对象。