【问题标题】:Notifications not shown in Android FroyoAndroid Froyo 中未显示通知
【发布时间】:2013-02-13 22:12:30
【问题描述】:

我正在尝试运行以下内容,该内容来自互联网教程:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Prepare intent which is triggered if the
    // notification is selected

    Intent homeIntent = new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory(Intent.CATEGORY_HOME);
    //Intent intent = new Intent(this, ReceiveAndGoHome.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, homeIntent, 0);


    // Build notification
    // Actions are just fake
    Notification noti = new NotificationCompat.Builder(this)
    .setContentTitle("Fraz Go Home!!!")
            .setContentTitle("Fraz Go Home!!!")
            .setContentText("Fraz Go Home!!!")
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_launcher, "Call", pIntent)
            .addAction(R.drawable.ic_launcher, "More", pIntent)
            .addAction(R.drawable.ic_launcher, "And more", pIntent).build();


    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;


    notificationManager.notify(25, noti); 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
}

当我在 froyo AVD 中运行上述内容时,我根本没有看到任何通知出现 - 我是否遗漏了一些明显的东西?

【问题讨论】:

    标签: java android android-2.2-froyo


    【解决方案1】:

    您忘记设置小图标。添加这个:

    .setSmallIcon (R.drawable.ic_launcher)
    

    在您的 NotificationCompat.Builder 方法链中的某个位置。

    当然,我在那里使用了可绘制的启动器,因为它就在手边,您需要为正确的 UI 制作实际的通知图标。这只是为了演示你需要的方法调用。

    Android documentation 概述了通知显示所需的内容:

    必需的通知内容

    通知对象必须包含 以下:

    • 一个小图标,由 setSmallIcon() 设置

    • 标题,由 setContentTitle() 设置

    • 详细文本,由 setContentText() 设置

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      相关资源
      最近更新 更多