Open top menu

How to implement Notification in android?


Let’s now take a peek at the Notification sample project,




How to implement Notification in android?
 about this tree structure i have created One Activity And two services class,shown below
MainActivity.java

package com.example.notification;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStartService = (Button)findViewById(R.id.start_Myservice);
        Button buttonStopService = (Button)findViewById(R.id.stop_Myservice);
        
        buttonStartService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, NotificationsService.class);
MainActivity.this.startService(intent);
}});
        
        buttonStopService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotificationsService.ACTION);
intent.putExtra("SERVICE", NotificationsService.STOP_SERVICE);
sendBroadcast(intent);
}});
        
}

@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;
}

}

You have created new Broadcast reciver class-->>>>>>>>>>

MyAction_Services.java

package com.example.notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyAction_Services extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context,NotificationsService.class);
      context.getApplicationContext().startService(i);
}

}

And last Notifcation services--->>>>>>>>>>>>>>>>>>>>>>

package com.example.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;

public class NotificationsService extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int REQUIRE_STOP_SERVICE = 1;
MyAction_Services notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://androidbeginnerpoint.blogspot.in/";
@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new MyAction_Services();
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
// Send Notification
notificationManager = 
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher, 
"Notification!", 
System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Exercise of Notification!";
String notificationText = "http://android-er.blogspot.com/";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent 
= PendingIntent.getActivity(getBaseContext(), 
0, myIntent, 
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, 
notificationTitle, 
notificationText, 
pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public class NotifyServiceReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("SERVICE", 0);
if (rqs == REQUIRE_STOP_SERVICE){
stopSelf();
}
              }
}
}

User interface created inside res/layout/main.xml-->>>>>>>>



 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world"
    />
<Button
    android:id="@+id/start_Myservice"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Start Service to send Notification"
    />
<Button
    android:id="@+id/stop_Myservice"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Stop Service"
    />
</LinearLayout>

Note-you can not forget given permission in Manifest file.

<service android:name=".NotificationsService"/>
 <receiver android:name=".MyAction_Services">

---------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notification"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notification.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <service android:name=".NotificationsService"/>
    <receiver android:name=".MyAction_Services">
    <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    </intent-filter>
    </receiver>
    </application>

</manifest>




Tagged

0 comments