如何在 android 中创建通知栏?

How to create notification bar in android?

我是 Android 编程新手。我创建了两个 类 IncomingSms.java & BroadcastNewSms.java代码:IncomingSms.java

package com.example.broadcastreceivernewsms;

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        //Uri uri = Uri.parse("content://sms");
        //String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
        //Cursor cursor = getContentResolver().query(uri, projection, "address='51555'", null, "date desc");

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);


                    // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context,
                            "senderNum: " + senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }
    }


}

代码:BroadcastNewSms.java

public class BroadcastNewSms extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast_new_sms);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_broadcast_new_sms, menu);
        return true;
    }

}

它有效 fine.When 我 运行 应用程序 & 每当新短信进入我的设备时,它都会将发件人号码和消息正文打印到 Toast 中。 现在我的问题是,如何在通知栏中创建一条消息 "A New message received"(就像每当我们在 Whatsapp 中收到一条短信时,它会在通知栏中打印一条通知,如果应用程序不是直接 运行ning ) 每当我的设备收到新消息时。

看下面的代码,有创建通知的例子:

要显示通知,您需要上下文对象

    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.your_ic_notification)
                .setContentTitle("tite"/*your notification title*/)
                .setContentText("Some example context string"/*notifcation message*/)
                .build();
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(1000/*some int*/, notification);
}