## Main Activity class ##
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view)
{
Intent i= new Intent(this,notificationreceiveractivity.class);
PendingIntent pi=PendingIntent.getActivity(this, 0,i, 0);
Notification not=new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.addAction(R.drawable.ic_launcher,"Call",pi)
.addAction(R.drawable.ic_launcher,"More",pi)
.addAction(R.drawable.ic_launcher,"And more",pi).build();
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
not.flags|=Notification.FLAG_AUTO_CANCEL;
nm.notify(0,not);
}
}
## Notification Receive Activity class ##
import android.app.Activity;
import android.os.Bundle;
public class notificationreceiveractivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}
## activity_main.xml ##
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="createNotification"
android:text="Create Notification" >
</Button>
</RelativeLayout>
## notification_message.xml ##
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the result activity opened from the notification" >
</TextView>
</RelativeLayout>
## manifest file ##
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationexamples"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".notificationreceiveractivity">
</activity>
</application>
</manifest>
Comments
Post a Comment