Created
September 28, 2016 10:49
-
-
Save mdzzohrabi/e1dca2165a981a7d16c15e5c7ed3bb11 to your computer and use it in GitHub Desktop.
Android Codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.basicnotifications; | |
import android.app.Activity; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.app.NotificationCompat; | |
import android.view.View; | |
/** | |
* The entry point to the BasicNotification sample. | |
*/ | |
public class MainActivity extends Activity { | |
/** | |
* A numeric value that identifies the notification that we'll be sending. | |
* This value needs to be unique within this app, but it doesn't need to be | |
* unique system-wide. | |
*/ | |
public static final int NOTIFICATION_ID = 1; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.sample_layout); | |
} | |
/** | |
* Send a sample notification using the NotificationCompat API. | |
*/ | |
public void sendNotification(View view) { | |
/** Create an intent that will be fired when the user clicks the notification. | |
* The intent needs to be packaged into a {@link android.app.PendingIntent} so that the | |
* notification service can fire it on our behalf. | |
*/ | |
Intent intent = new Intent(Intent.ACTION_VIEW, | |
Uri.parse("http://developer.android.com/reference/android/app/Notification.html")); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); | |
/** | |
* Use NotificationCompat.Builder to set up our notification. | |
*/ | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this); | |
/** Set the icon that will appear in the notification bar. This icon also appears | |
* in the lower right hand corner of the notification itself. | |
* | |
* Important note: although you can use any drawable as the small icon, Android | |
* design guidelines state that the icon should be simple and monochrome. Full-color | |
* bitmaps or busy images don't render well on smaller screens and can end up | |
* confusing the user. | |
*/ | |
builder.setSmallIcon(R.drawable.ic_stat_notification); | |
// Set the intent that will fire when the user taps the notification. | |
builder.setContentIntent(pendingIntent); | |
// Set the notification to auto-cancel. This means that the notification will disappear | |
// after the user taps it, rather than remaining until it's explicitly dismissed. | |
builder.setAutoCancel(true); | |
/** | |
*Build the notification's appearance. | |
* Set the large icon, which appears on the left of the notification. In this | |
* sample we'll set the large icon to be the same as our app icon. The app icon is a | |
* reasonable default if you don't have anything more compelling to use as an icon. | |
*/ | |
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); | |
/** | |
* Set the text of the notification. This sample sets the three most commononly used | |
* text areas: | |
* 1. The content title, which appears in large type at the top of the notification | |
* 2. The content text, which appears in smaller text below the title | |
* 3. The subtext, which appears under the text on newer devices. Devices running | |
* versions of Android prior to 4.2 will ignore this field, so don't use it for | |
* anything vital! | |
*/ | |
builder.setContentTitle("BasicNotifications Sample"); | |
builder.setContentText("Time to learn about notifications!"); | |
builder.setSubText("Tap to view documentation about notifications."); | |
/** | |
* Send the notification. This will immediately display the notification icon in the | |
* notification bar. | |
*/ | |
NotificationManager notificationManager = (NotificationManager) getSystemService( | |
NOTIFICATION_SERVICE); | |
notificationManager.notify(NOTIFICATION_ID, builder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment