package com.example.notificationactivity;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
private static final int NOTE_ID=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.postDelayed(task, 1000);
Toast toast=Toast.makeText(getApplicationContext(), "notification will be displayed in 10 sec.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
}
Handler handler=new Handler();
private Runnable task=new Runnable() {
@Override
public void run() {
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Something Happened")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentTitle("We're Finished!")
.setContentText("Click Here!")
.setContentIntent(pi);
/* builder.addAction(R.drawable.ic_launcher, "Call Back", pi);
builder.addAction(R.drawable.ic_launcher, "Call History", pi);
NotificationCompat.BigTextStyle expandedStyle =
new NotificationCompat.BigTextStyle(builder);
expandedStyle.bigText("Here is some additional text to be displayed when"
+ " the notification is in expanded mode. "
+ " I can fit so much more content into this giant view!"); */
NotificationCompat.InboxStyle expandedStyle =
new NotificationCompat.InboxStyle(builder);
expandedStyle.setSummaryText("4 New Tasks");
expandedStyle.addLine("Make Dinner");
expandedStyle.addLine("Call Mom");
expandedStyle.addLine("Call Wife First");
expandedStyle.addLine("Pick up Kids");
// Notification note = expandedStyle.build();
Notification note = builder.build();
//Post the notification
manager.notify(NOTE_ID, note);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
private static final int NOTE_ID=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.postDelayed(task, 1000);
Toast toast=Toast.makeText(getApplicationContext(), "notification will be displayed in 10 sec.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
}
Handler handler=new Handler();
private Runnable task=new Runnable() {
@Override
public void run() {
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Something Happened")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentTitle("We're Finished!")
.setContentText("Click Here!")
.setContentIntent(pi);
/* builder.addAction(R.drawable.ic_launcher, "Call Back", pi);
builder.addAction(R.drawable.ic_launcher, "Call History", pi);
NotificationCompat.BigTextStyle expandedStyle =
new NotificationCompat.BigTextStyle(builder);
expandedStyle.bigText("Here is some additional text to be displayed when"
+ " the notification is in expanded mode. "
+ " I can fit so much more content into this giant view!"); */
NotificationCompat.InboxStyle expandedStyle =
new NotificationCompat.InboxStyle(builder);
expandedStyle.setSummaryText("4 New Tasks");
expandedStyle.addLine("Make Dinner");
expandedStyle.addLine("Call Mom");
expandedStyle.addLine("Call Wife First");
expandedStyle.addLine("Pick up Kids");
// Notification note = expandedStyle.build();
Notification note = builder.build();
//Post the notification
manager.notify(NOTE_ID, note);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
No comments:
Post a Comment