2014-12-26 3 views
-1

전화 상태를 모니터링하고 상태가 변경되면 축배 메시지를 표시하는 백그라운드 프로세스를 만들려고합니다. 지금까지 필자는 이것을 구현했지만, 설치하고 실행할 때. 그것은 아무것도하지 않습니다. 그것은 아무것도 표시하지 않을 것이다. 내 코드를 여기에 게시하고 있습니다. pls 날 도와.android : 배경 전화 상태 모니터링

StateMonitor.java

`

import android.content.Context; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 

public class PhoneStateMonitor extends PhoneStateListener { 
    Context context; 

    public PhoneStateMonitor(Context context) { 
     super(); 
     // TODO Auto-generated constructor stub 
     this.context=context; 
    } 

    //This Method Automatically called when changes is detected in Phone State 
    public void onCallStateChanged(int state, String incomingNumber) { 
     // TODO Auto-generated method stub 
     super.onCallStateChanged(state, incomingNumber); 

     Toast.makeText(context, "Phone State - "+state+" Incoming Number - "+incomingNumber, Toast.LENGTH_LONG).show();//Giving the Message that Phone State Changed 
     //Checking The phone state 
     switch(state) 
     { 
     case TelephonyManager.CALL_STATE_IDLE: //Phone is in Idle State 
      Toast.makeText(context, "Phone State is IDLE", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_RINGING: //Phone is Ringing 
      Toast.makeText(context, "Phone State is RINGING", Toast.LENGTH_LONG).show(); 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: //Call is Received 
      Toast.makeText(context, "Call State is OFFHOOK",Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 
} 
` 

PhoneReceiver.java는

`import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
//Automatically called when State Change is Detected because this Receiver is Registered for PHONE_STATE intent filter in AndroidManifest.xml 
public class PhoneStateReceiver extends BroadcastReceiver { 

    TelephonyManager manager;  
    PhoneStateMonitor phoneStateListener; 
    static boolean isAlreadyListening=false; 

    //This Method automatically Executed when Phone State Change is Detected 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     phoneStateListener =new PhoneStateMonitor(context);//Creating the Object of Listener 
     manager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);//Getting the Telephony Service Object 
     if(!isAlreadyListening)//Checking Listener is Not Registered with Telephony Services 
     { 
       manager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);//Registering the Listener with Telephony to listen the State Change 
      isAlreadyListening=true; //setting true to indicate that Listener is listening the Phone State 
     } 

    } 

} 
` 

안드로이드 Manifest.xml는

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

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="21" /> 

<uses-permission android:name="android.permission.READ_PHONE_STATE" /><!-- Permission for Reading Phone State --> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 

     <!-- If this app is not running then this will be automatically called when phone state change detected(Look at Intent Filter) --> 
     <receiver android:name=".PhoneStateReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.PHONE_STATE"/> 
      </intent-filter> 
      </receiver> 
    </application> 

</manifest>` 

이클립스에서 설치, 그것은 APK 설치 말한다 . 내 전화를 부를 때 토스트 메시지가 나옵니다. 나는 어디로 잘못 가고 있니? 도와주세요! 감사!

답변

1

앱을 설치하면 일부 UI 컨트롤 (예 : Activity)으로 애플리케이션을 시작할 때까지 앱이 stopped 상태로 유지됩니다. Android 3.1 APIs (API 12)에서 의무화되었습니다.

궁극적으로 BroadcastReceiver 외의 상태에서 앱을 이동하는 것 외에도 애플리케이션에 하나 이상의 Activity이 필요합니다. 그렇지 않으면 브로드 캐스트를 수신하지 않습니다.

참고 : 다음

Launch Controls

+0

어떻게 내가 같은의 배경 서비스를 작성하려면 어떻게해야합니까? 설치 한 후에는 모니터 만하면됩니다. 숨겨진 활동이나 창이있을 수 있습니까? –

+0

당신은 그 목적을 위해 동일한 코드를 사용할 수 있지만, 당신이 필요로하는 것은 앱을 시작할 수있는 적어도 하나의'Activity'입니다. –

+0

그래서 내 활동을 숨기려면 어떻게해야합니까? –