2013-07-28 5 views

답변

1

BroadcastReceiver를 시작하거나 중지 할 필요가 없습니다. 귀하의 백그라운드 운영 서비스가 아닙니다. 필요한 것은 응용 프로그램에 등록 또는 등록 취소 만하면됩니다. 일단 등록되면, 항상 켜져 있습니다.

특정 이벤트가 발생하면 시스템은 등록 된 모든 응용 프로그램에 이벤트를 알립니다 (브로드 캐스트). 등록 된 모든 앱은 Intent으로 표시됩니다. 또한, 당신은 자신의 방송을 보낼 수 있습니다. 이상

, see this

간단한 예 :

내 매니페스트

, 내가

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.android.receivercall" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="10" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <receiver android:name=".Main"> 
     <intent-filter > 
      <action android:name="android.intent.action.PHONE_STATE"/> 
     </intent-filter> 
    </receiver> 
    <activity android:name=".TelServices"> 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 

     </intent-filter> 
    </activity> 
</application> 

이제 권한 및 수신기를 포함하고있어, 내 수신기, 홈페이지 .java

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 



    public class Main extends BroadcastReceiver { 
    String number,state; 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     state=intent.getStringExtra(TelephonyManager.EXTRA_STATE); 

     if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
      number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 

      Toast.makeText(context, "Call from : "+number, Toast.LENGTH_LONG).show(); 
     } 
     else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)) 
      Toast.makeText(context, "Call ended", Toast.LENGTH_LONG).show(); 
     else 
      Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show(); 

    } 

} 

여기에서이 앱을 설치하면 매니페스트를 통해 브로드 캐스트 수신기를 등록합니다. 전화가 올 때/끝날 때마다 토스트가 나타납니다.

+0

대단히 감사합니다. – nirbe

+0

오신 것을 환영합니다 .. – Nizam

관련 문제