如何在android中创建通知栏?

时间:2015-01-10 20:40:00

标签: android android-notification-bar

我是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;
    }

}

它工作正常。当我运行应用程序&amp;每当有新的短信进入我的设备时,它会打印发件人号码&amp;消息体进入Toast。   现在我的问题是,如何在通知栏中创建一条消息&#34;收到一条新消息&#34;(就像我们在Whatsapp中收到短信一样,如果应用程序没有运行,它打印会在notificatiobar中发出通知直接)每当有新消息收到我的设备时。

1 个答案:

答案 0 :(得分:1)

查看下面的代码,有一些示例如何创建通知:

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

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