【问题标题】:on button click notification按钮点击通知
【发布时间】:2014-05-27 03:48:13
【问题描述】:

我正在使用 Eclipse IDE 在 android 中创建应用程序,我想在我的 android 中实现通知我想在按钮单击示例上发出通知我想通知保存的数据..我发现这个教程 here 我复制了代码并将其粘贴到我的 setOnClickListener 代码中..但我收到错误..我不知道如何开始这样做,因为在教程中他们只是给你代码 sn-p..这是我的代码

更新问题

通知和推送通知有什么区别,哪个更好用..

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();
                }
            });

【问题讨论】:

  • 然后发布你的 logcat。
  • 我还没有日志猫,因为我仍然有错误,实际上我正在这样做并开始通过阅读其他教程来消除一些错误,但是一些错误不会消失..我有可能遗漏了什么这里
  • 是的,这是可能的,但你得到了什么错误?
  • 我在 mNotifyManager 和 mBuilder 中出现错误,我需要把它放在 oncreate 这是我唯一的代码..
  • 好的,您只需在按钮 onclick 外部创建此对象

标签: android android-intent notifications push-notification


【解决方案1】:

你应该改变这个

 mBuilder = new NotificationCompat.Builder(this);

 mBuilder = new NotificationCompat.Builder(youractivity.this);

您需要传递当前的Context 来创建mBuilder 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多