Using NotificationManager : Notification « Core Class « Android






Using NotificationManager

  


package app.test;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

class TestService extends Service {

  private static final String TAG = "TestService";
  private NotificationManager _nm;

  @Override
  public IBinder onBind(Intent i) {
    Log.e(TAG, "TestService.onBind");
    return null;
  }

  public class LocalBinder extends Binder {
    TestService getService() {
      return TestService.this;
    }
  }

  @Override
  public boolean onUnbind(Intent i) {
    Log.e(TAG, "TestService.onUnbind");
    return false;
  }

  @Override
  public void onRebind(Intent i) {
    Log.e(TAG, "TestService.onRebind");
  }

  @Override
  public void onCreate() {
    Log.e(TAG, "TestService.onCreate");
    _nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    showNotification();
  }

  @Override
  public void onStart(Intent intent, int startId) {
    Log.e(TAG, "TestService.onStart");
  }

  @Override
  public void onDestroy() {
    _nm.cancel(0);
    Log.e(TAG, "TestService.onDestroy");
  }

  private void showNotification() {
    Notification notification = new Notification(R.drawable.icon,
        "Service started", System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, Test.class), 0);
    notification.setLatestEventInfo(this, "Test Service",
        "Service started", contentIntent);
    _nm.notify(0, notification);
  }
}

public class Test extends Activity {
  private boolean _isBound;
  private TestService _boundService;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setTitle("Service Test");

    initButtons();

  }
  
    private ServiceConnection _connection = new ServiceConnection() {  
        public void onServiceConnected(ComponentName className, IBinder service) {           
            _boundService = ((TestService.LocalBinder)service).getService();  
              
            Toast.makeText(Test.this, "Service connected",  
                    Toast.LENGTH_SHORT).show();  
        }  
  
        public void onServiceDisconnected(ComponentName className) {  
            _boundService = null;  
            Toast.makeText(Test.this, "Service connected",  
                    Toast.LENGTH_SHORT).show();  
        }  
    };  
    
    private void initButtons() {  
        Button buttonStart = (Button) findViewById(R.id.start_service);  
        buttonStart.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                startService();  
            }  
        });  
  
        Button buttonStop = (Button) findViewById(R.id.stop_service);  
        buttonStop.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                stopService();  
            }  
        });  
  
        Button buttonBind = (Button) findViewById(R.id.bind_service);  
        buttonBind.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                bindService();  
            }  
        });  
  
        Button buttonUnbind = (Button) findViewById(R.id.unbind_service);  
        buttonUnbind.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
                unbindService();  
            }  
        });  
    }  
  
    private void startService() {  
        Intent i = new Intent(this, TestService.class);  
        this.startService(i);  
    }  
    
    private void stopService() {  
        Intent i = new Intent(this, TestService.class);  
        this.stopService(i);  
    }  
  
    private void bindService() {  
        Intent i = new Intent(this, TestService.class);  
         bindService(i, _connection, Context.BIND_AUTO_CREATE);  
         _isBound = true;  
    }  
  
    private void unbindService() {  
        if (_isBound) {  
            unbindService(_connection);  
            _isBound = false;  
        }  
    }  
}

//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" />


  <Button android:id="@+id/start_service" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Start Service" />

  <Button android:id="@+id/stop_service" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Stop Service" />

  <Button android:id="@+id/bind_service" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Bind Service" />

  <Button android:id="@+id/unbind_service" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Unbind Service" />

</LinearLayout>

   
    
  








Related examples in the same category

1.Notification event
2.How to use Notification
3.Look up the notification manager service
4.Cancel the notification that we started
5.Demonstrates adding notifications to the status bar
6.Send Notification