2014-12-26 36 views
-1

我正在嘗試創建一個後臺進程,用於監控電話狀態並在狀態發生變化時顯示Toast消息。到目前爲止,我實施了這些,但是當我安裝並運行。它什麼也沒做。它不會顯示任何東西。我在這裏發佈我的代碼。請幫助我。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 
     } 

    } 

} 
` 

的Android的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>` 

當我從Eclipse的安裝,它說安裝APK 。當我撥打我的電話時,Toast消息不會彈出。我哪裏錯了?請幫忙!謝謝!

回答

1

當您安裝應用程序時,它將保持在stopped狀態,直到您通過一些UI控件(即Activity)啓動應用程序。它從Android 3.1 APIs (API 12)變成強制性的。因此,最終,除了BroadcastReceiver之外,您的應用程序中至少需要一個Activity,才能將您的應用程序移出stopped狀態。否則它不會收到任何廣播。

參考:

Launch Controls

+0

那麼我怎樣寫一個相同的後臺服務?我安裝後,它應該只監視。隱藏的活動或窗口可能是? –

+0

你可以使用相同的代碼,但你需要的是至少一個可以啓動你的應用程序的Activity。 –

+0

那麼我如何隱藏我的活動? –