29

我有以下用于 android 应用程序的代码:

package com.authorwjf.e_notifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.avatar), 
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height), 
                true);
        Intent intent = new Intent(this, Main.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentTitle("This is the title");
        builder.setContentText("This is the text");
        builder.setSubText("Some sub text");
        builder.setNumber(101);
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Fancy Notification");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setLargeIcon(bm);
        builder.setAutoCancel(true);
        builder.setPriority(0);
        Notification notification = builder.build();
        NotificationManager notificationManger = 
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManger.notify(01, notification);        
    }

}

基本上应用程序在启动时会创建一个通知,现在我有几个问题:

  • 是否有可能在启动应用程序时,通知显示在下拉通知列表中,但状态栏中没有图标。IE。参考

http://imagebin.org/226494

红圈的图标可以不显示吗?最终,我想创建一个仅位于下拉通知中的服务。

  • 是否可以使下拉通知中的通知持续存在,即。如果按下蓝色圆圈(http://imagebin.org/226494)的图标,通知仍然存在?

我是 android dev 的新手,只是想了解我可以用通知做什么。

谢谢

4

3 回答 3

58
  • A1:要删除状态栏图标,请使用以下技巧:

    builder.setSmallIcon(android.R.color.transparent); //Tested and worked in API 14
    
  • A2:要进行持久通知,请添加以下行:

    builder.setOngoing(true)
    
于 2012-08-30T22:37:49.093 回答
3

看看正在进行的事件标志。我相信这可以创建类似于系统的 wifi 和 USB 连接通知的持续通知。

于 2012-08-30T21:12:41.967 回答
0
  public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Hello  Toast", Toast.LENGTH_SHORT).show();
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
       /* Intent launchIntent =context.getPackageManager().getLaunchIntentForPackage("com.sonisony.clarityitapp");
            context.startActivity(launchIntent);*/
        Intent i = new Intent(context, CheckAppActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,i,0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"TestAndro")
                .setContentTitle("Andro Open Test Alert")
                .setContentText("Hello Hello ...........")
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
//                for Persistent
                .setOngoing(true);



        NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context);
        notificationCompat.notify(123,builder.build());
    }
}
于 2021-11-16T08:06:37.873 回答