Reputation: 59
I need to start service when my device change cell connection and I presume to do that with onCellLocationChanged()
implements below but the beeps start only if I receive incoming calls...
my listener:
public class CellLocationListener extends PhoneStateListener {
@Override
public void onCellLocationChanged(CellLocation location) {
super.onCellLocationChanged(location);
int cid = 0;
int lac = 0;
if (location != null) {
if (location instanceof GsmCellLocation) {
cid = ((GsmCellLocation) location).getCid();
lac = ((GsmCellLocation) location).getLac();
}
else if (location instanceof CdmaCellLocation) {
cid = ((CdmaCellLocation) location).getBaseStationId();
lac = ((CdmaCellLocation) location).getSystemId();
}
}
String cellInfo = Integer.toString(lac)+"-"+Integer.toString(cid);
Log.v("logg","CELL CHANGED:"+cellInfo);
}
}
I've used the same cellInfo on mainactivity and when I start it cellInfo chages its value but no beeps from broadcas receiver....
on manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<receiver android:name="dado.auto3gdataswitch.CellChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
my broadcastReceiver
public class CellChangeReceiver extends BroadcastReceiver {
TelephonyManager telephony;
public void onReceive(Context context, Intent intent) {
CellLocationListener phoneListener = new CellLocationListener();
telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, CellLocationListener.LISTEN_CALL_STATE);
Toast.makeText(context, "ON receiver", Toast.LENGTH_LONG).show();
final MediaPlayer mp = MediaPlayer.create(context, R.raw.beep);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
Log.v("logg","onreceiver");
}
}
I hear a beep only if I receive incoming call and not if I change cell....
What's wrong??
Upvotes: 2
Views: 3156
Reputation: 59
I've foud a better solution!!!
in my mainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TelephonyManager telm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CELL_LOCATION;
telm.listen(phoneStateListener, events);
.....
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCellLocationChanged(CellLocation location) {
super.onCellLocationChanged(location);
int cid = 0;
int lac = 0;
if (location != null) {
if (location instanceof GsmCellLocation) {
cid = ((GsmCellLocation) location).getCid();
lac = ((GsmCellLocation) location).getLac();
}
else if (location instanceof CdmaCellLocation) {
cid = ((CdmaCellLocation) location).getBaseStationId();
lac = ((CdmaCellLocation) location).getSystemId();
}
}
String cellBase = Integer.toString(lac)+"-"+Integer.toString(cid);
Toast.makeText(getBaseContext(), cellBase, Toast.LENGTH_LONG).show();
Log.v("logg", "cell:"+cellBase);
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.beep);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
}
};
Upvotes: 0
Reputation: 11244
You are listening to CellLocationListener.LISTEN_CALL_STATE
, but you should listen for CellLocationListener.LISTEN_CELL_LOCATION
. And if you want the sound to be played on cell change, you should move the corresponding code to the onCellLocationChanged
. And your cell change listener will be registered only when android.intent.action.PHONE_STATE
will be received.
Upvotes: 4