Here i represented a way to show some notiffication on status bar, it is simple and easy to understand. please make a project and copy the give code and test with the device.
MainActivity .java
package com.jitesh.androidpassingprimitives;
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.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private NotificationManager mNManager;
private static final int NOTIFY_ID = 1100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String ns = Context.NOTIFICATION_SERVICE;
mNManager = (NotificationManager) getSystemService(ns);
final Notification msg = new Notification(R.drawable.ic_action_search,
"New event of importance", System.currentTimeMillis());
Button start = (Button) findViewById(R.id.start);
Button cancel = (Button) findViewById(R.id.cancel);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "ShowNotification Example";
CharSequence contentText = "Browse Jitesh upadhyay's Android Blog Site";
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://upadhyayjiteshandroid.blogspot.in/"));
PendingIntent intent = PendingIntent.getActivity(
MainActivity.this, 0, msgIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
msg.defaults |= Notification.DEFAULT_SOUND;
msg.flags |= Notification.FLAG_AUTO_CANCEL;
msg.setLatestEventInfo(context, contentTitle, contentText,
intent);
mNManager.notify(NOTIFY_ID, msg);
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mNManager.cancel(NOTIFY_ID);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startscreen_text"
android:layout_alignRight="@+id/startscreen_text"
android:layout_marginBottom="55dp"
android:layout_marginRight="34dp"
android:text="Button" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startscreen_text"
android:layout_alignRight="@+id/startscreen_text"
android:layout_marginBottom="55dp"
android:layout_marginRight="34dp"
android:text="Button" />
</LinearLayout>
MainActivity .java
package com.jitesh.androidpassingprimitives;
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.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private NotificationManager mNManager;
private static final int NOTIFY_ID = 1100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String ns = Context.NOTIFICATION_SERVICE;
mNManager = (NotificationManager) getSystemService(ns);
final Notification msg = new Notification(R.drawable.ic_action_search,
"New event of importance", System.currentTimeMillis());
Button start = (Button) findViewById(R.id.start);
Button cancel = (Button) findViewById(R.id.cancel);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "ShowNotification Example";
CharSequence contentText = "Browse Jitesh upadhyay's Android Blog Site";
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://upadhyayjiteshandroid.blogspot.in/"));
PendingIntent intent = PendingIntent.getActivity(
MainActivity.this, 0, msgIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
msg.defaults |= Notification.DEFAULT_SOUND;
msg.flags |= Notification.FLAG_AUTO_CANCEL;
msg.setLatestEventInfo(context, contentTitle, contentText,
intent);
mNManager.notify(NOTIFY_ID, msg);
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mNManager.cancel(NOTIFY_ID);
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startscreen_text"
android:layout_alignRight="@+id/startscreen_text"
android:layout_marginBottom="55dp"
android:layout_marginRight="34dp"
android:text="Button" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/startscreen_text"
android:layout_alignRight="@+id/startscreen_text"
android:layout_marginBottom="55dp"
android:layout_marginRight="34dp"
android:text="Button" />
</LinearLayout>
Nice post with great details. I really appreciate your info. desktop alerting software
ReplyDelete