Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.myapplication;
- import android.annotation.SuppressLint;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.RemoteViews;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- new MyNotification(this, R.layout.notification_layout);
- }
- }
- @SuppressLint("ParcelCreator")
- class MyNotification extends Notification {
- private Context ctx;
- public Context getCtx() {
- return ctx;
- }
- private NotificationManager mNotificationManager;
- public MyNotification(Context ctx, int layout_id) {
- super();
- this.ctx = ctx;
- String ns = Context.NOTIFICATION_SERVICE;
- mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
- CharSequence tickerText = "Shortcuts";
- long when = System.currentTimeMillis();
- Notification.Builder builder = new Notification.Builder(ctx);
- Notification notification = builder.getNotification();
- notification.when = when;
- notification.tickerText = tickerText;
- notification.icon = R.mipmap.ic_launcher;
- RemoteViews contentView = new RemoteViews(ctx.getPackageName(), layout_id);
- //set button listners
- setListeners(contentView);
- notification.contentView = contentView;
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- mNotificationManager.notify(1387, notification);
- }
- private void setListeners(RemoteViews contentView) {
- RemoteViews button = new RemoteViews(ctx.getPackageName(), R.layout.image_btn_layout_test);
- Intent actionIntent = new Intent("MyIntent");
- PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, actionIntent, 0);
- button.setOnClickPendingIntent(R.id.image, pendingIntent);
- contentView.addView(R.id.root, button);
- }
- }
Add Comment
Please, Sign In to add comment