AndroidManifest.xml
- Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest package="tk.d3xt3r01" android:versionCode="1" android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".helloworld" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
MyPhoneStateListener.java
- Code: Select all
package tk.d3xt3r01;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneStateListener extends PhoneStateListener {
private String TAG = "d3x";
@Override
public void onCallStateChanged(int state,String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_OFFHOOK "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING "+incomingNumber);
break;
default:
Log.i(TAG, "MyPhoneStateListener->onCallStateChanged() -> default -> "+Integer.toString(state));
break;
}
}
@Override
public void onServiceStateChanged (ServiceState serviceState){
switch(serviceState.getState()){
case ServiceState.STATE_IN_SERVICE:
Log.i(TAG, "MyPhoneStateListener->onServiceStateChanged() -> STATE_IN_SERVICE");
serviceState.setState(ServiceState.STATE_IN_SERVICE);
break;
case ServiceState.STATE_OUT_OF_SERVICE:
Log.i(TAG, "MyPhoneStateListener->onServiceStateChanged() -> STATE_OUT_OF_SERVICE");
serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
break;
case ServiceState.STATE_EMERGENCY_ONLY:
Log.i(TAG, "MyPhoneStateListener->onServiceStateChanged() -> STATE_EMERGENCY_ONLY");
serviceState.setState(ServiceState.STATE_EMERGENCY_ONLY);
break;
case ServiceState.STATE_POWER_OFF:
Log.i(TAG, "MyPhoneStateListener->onServiceStateChanged() -> STATE_POWER_OFF");
serviceState.setState(ServiceState.STATE_POWER_OFF);
break;
default:
Log.i(TAG, "MyPhoneStateListener->onServiceStateChanged() -> default -> "+Integer.toString(serviceState.getState()));
break;
}
}
}
ServiceReceiver.java
- Code: Select all
package tk.d3xt3r01;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ServiceReceiver extends BroadcastReceiver {
private String TAG = "d3x";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "ServiceReceiver->onReceive();");
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_SERVICE_STATE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
But this isn't done right .. I'm sure of it ..
Edit 1:
What I want is just a Log.i(); to notice me when the phone got registered to the network/emergy only/in service ...
but as I understood ServiceState only stores a snapshot of the state and I have to register a listener to callback the function and set the new state .. :\ which I kinda' did .. Any ideas ?
but what about the status when I start my app ?! why is the default 1 (OUT_OF_SERVICE) and why doesn't it get updated ?!
Edit 2:
onReceive() is only called on an intent .. and the service state isn't an intent .. :/ I don't get it .. How am I suppsed to get the current state of the registration (cellural) status

Edit 3:
Well, I found out that the way I was testing was wrong !
I launched the app, press home ( app goes to onPause()->onStop() ) then settings->network->airplane .. and when it's on stop .. it's stopped .. it doesn't actually do anything .. but when in the app itself .. I tried loosing signal (covered the phone with some metal stuff .. ) it worked ! the problem now is that I get this thing ( onreceive called too many times each time it's called ..
I/d3x ( 4622): onCreate();
I/d3x ( 4622): OpenHelper();
I/d3x ( 4622): DataBaseWork(insert into users(name,number) values (?,?));
I/d3x ( 4622): DataBaseWork(delete from users WHERE _id = ?);
I/d3x ( 4622): Current network state: 1
I/d3x ( 4622): selectAll();
I/d3x ( 4622): onStart();
I/d3x ( 4622): onResume();
I/d3x ( 4622): onPause();
I/d3x ( 4622): onResume();
I/d3x ( 4622): onPause();
[phone got to sleep]
I/d3x ( 4622): onResume();
[woke him up]
I/d3x ( 4622): ServiceReceiver->onReceive();
I/d3x ( 4622): MyPhoneStateListener->onServiceStateChanged() -> STATE_IN_SERVICE
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING +0000000000
I/d3x ( 4622): onPause();
I/d3x ( 4622): onStop();
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): ServiceReceiver->onReceive();
I/d3x ( 4622): MyPhoneStateListener->onServiceStateChanged() -> STATE_IN_SERVICE
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): onRestart();
I/d3x ( 4622): onStart();
I/d3x ( 4622): onResume();
[called myself]
I/d3x ( 4622): onPause();
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING +0000000000
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING +0000000000
I/d3x ( 4622): ServiceReceiver->onReceive();
I/d3x ( 4622): MyPhoneStateListener->onServiceStateChanged() -> STATE_IN_SERVICE
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING +0000000000
I/d3x ( 4622): onResume();
I/d3x ( 4622): onPause();
I/d3x ( 4622): onStop();
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): ServiceReceiver->onReceive();
I/d3x ( 4622): MyPhoneStateListener->onServiceStateChanged() -> STATE_IN_SERVICE
I/d3x ( 4622): MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE +0000000000
I/d3x ( 4622): onRestart();
I/d3x ( 4622): onStart();
I/d3x ( 4622): onResume();
[called myself again]
notice the on oncallstatechangedpart ? !? each time I call once it gets bigger !


