I am trying to make a MissCall application that sends a message automatically when I receive a missed call. I completed my application and everything worked out fine!
Here is the full scenario:
Problem: The
application worked fine , but when I restarted the device, the application did not work! . It only worked when I started my application at least once, after it worked fine until it shuts down.
Here is my code:
package com.example.misscallapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class Pref_Main extends PreferenceActivity {
int checkIt = 0;
TelephonyManager tm;
CallStateListener callStateListener = new CallStateListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
tm = (TelephonyManager) getBaseContext().getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(getBaseContext(), "Incoming: " + incomingNumber,
Toast.LENGTH_LONG).show();
checkIt = 1;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
checkIt = 0;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (checkIt == 1) {
Intent i = new Intent(getBaseContext(),MyService.class);
i.putExtra("phno", incomingNumber);
startService(i);
}
break;
}
}
}
}
:
, - BroadcastReceiver. BroadcastReceiver, PhoneStateListener
, , , BroadcastReceivers , - PhoneStateListener, onCallStateChanged , :
package com.example.misscallapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallReceiverBroadcast extends BroadcastReceiver {
int checkIt = 0;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
String incomingNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context , "Incoming: " + incomingNumber,
Toast.LENGTH_LONG).show();
checkIt = 1;
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
checkIt = 0;
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
if (checkIt == 1) {
Toast.makeText(context , "This is not shown ",
Toast.LENGTH_LONG).show();
Intent i = new Intent(context,MyService.class);
i.putExtra("phno", incomingNumber);
context.startService(i);
}
}
}
}
}
, , :
package com.example.misscallapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallReceiverBroadcast extends BroadcastReceiver {
int checkIt = 0;
Context contextt;
TelephonyManager tm;
CallStateListener callStateListener = new CallStateListener();
@Override
public void onReceive(Context context, Intent intent) {
contextt = context;
tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(contextt, "Incoming: " + incomingNumber,
Toast.LENGTH_LONG).show();
checkIt = 1;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
checkIt = 0;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (checkIt == 1) {
break;
}
}
}
}
}
, , , 1, , 10 !
,
.
1:
, , onReceive() TelphoneManager instance is created Phoone.
:
CallReceiverBroadcast static! !! , , , , 2 , , . - , , , .