Program/Android Java

BroadcastReceiver

너구리V 2011. 10. 23. 01:26

BroadcastReceiver : 핸드폰에서i 발생하는 특정 intent 신호를 받아주는 클래스
다음 예제는 핸드폰을 이용하여 outgoing call를 했을때 (전화를 걸때) 발생하는 intent를 감지하여
Service(백그라운드)로 Notification을 발생하는 것이다.


1) myBro.class
package sun.mybro;

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

public class myBro extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
 
  context.startService(new Intent(context, MyService.class));
   }
}

2. MyService.class
package sun.mybro;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;

public class MyService extends Service {

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

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
   String ns = Context.NOTIFICATION_SERVICE;
   NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  int icon = R.drawable.icon;
  CharSequence tickerText = "Hello";
  long when = System.currentTimeMillis();
  Notification notification = new Notification(icon, tickerText, when);
  Context context = getApplicationContext();
  CharSequence contentTitle = "My notification";
  CharSequence contentText = "Hello World!";
  Intent notificationIntent = new Intent(this, MyService.class);
  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  final int HELLO_ID = 1;
  mNotificationManager.notify(HELLO_ID, notification);
  
 }
 @Override
 public void onDestroy() {

  NotificationManager mNotificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
  mNotificationManager.cancelAll();
  super.onDestroy();
 }

3. Menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sun.mybro"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
  
    <receiver android:name="myBro">
     <intent-filter>
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
     </intent-filter>
    </receiver>
<service android:name="MyService" android:enabled="true"></service>
</application>
    <uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
</manifest>

Application nodes에서
Receiver와 Service를 추가한다.
Permission에서 Uses permission에서 PROCESS.OUT_GOING_CALL을 선택한다.

어플리케이션을 실행하고 아무 전화번호로 전화를 걸면
BroadcastReceiver가 OUTGOING_CALL intent신호를 받아서 실행되고
Service를 동작시켜서 백그라운드로 Notification이 실행된다.

반응형