android simple notification example
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".Notification"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Notification" /> </RelativeLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.NotificationManager; import android.content.Context; import android.content.res.Resources; import android.graphics.BitmapFactory; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.graphics.Bitmap; public class Notification extends AppCompatActivity { private Context mContext; private Resources mResources; private RelativeLayout mRelativeLayout; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); mContext = getApplicationContext(); mResources = getResources(); mRelativeLayout = (RelativeLayout) findViewById(R.id.rl); mButton = (Button) findViewById(R.id.btn); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); builder.setSmallIcon(R.drawable.android); Bitmap bitmap = BitmapFactory.decodeResource(mResources, R.drawable.android); builder.setLargeIcon(bitmap); builder.setContentTitle("Android Studio Pro"); builder.setContentText("Learn Android App Development"); int notificationId = 001; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(notificationId, builder.build()); } }); } } |