Click here to Skip to main content
16,018,394 members

Comments by user1992 (Top 8 by date)

user1992 1-Dec-13 14:54pm View    
how include onReceive() in my code so that when call incoming have to be reject with a automatic message.??

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING)
{

Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();
contactExists(getApplicationContext(),incomingNumber);



}
if(state==TelephonyManager.CALL_STATE_OFFHOOK)
{
Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
}

if(state==TelephonyManager.CALL_STATE_IDLE)
{
Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

}




public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
user1992 1-Dec-13 9:58am View    
Thanks bharat Parsiya,

I try this example and my code has crash. I think that the problem is because i use:
( https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/)
import com.android.internal.telephony.ITelephony;
Do you have in mind another solution for this problem ??
Maybe by using official android SDK?
user1992 18-Nov-13 16:19pm View    
in start has a text NOT DETECTING
after that it haw 2 buttons
the first is - Turn on
and the last is - exit

when i press Turn on the button message become turn off and the starting message become DETECTING
Finally when i press exit the starting message become NOT DETECTING and the app is exit.

i run this code on simulator so i don't know if the output was different if i had device..
user1992 18-Nov-13 16:10pm View    
Deleted
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

Toast.makeText(ctx,
"Outgoing: "+number,
Toast.LENGTH_LONG).show();
}

}

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;

private OutgoingReceiver outgoingReceiver;

public CallHelper(Context ctx) {
this.ctx = ctx;

callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}

/**
* Start calls detection.
*/
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);
}

/**
* Stop calls detection.
*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
ctx.unregisterReceiver(outgoingReceiver);
}

}
ListenCallExample Manifest
--------------------------

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
="" package="com.example.listencallexample" android:versioncode="1" android:versionname="1.0">

<uses-sdk
android:minsdkversion="8"
="" android:targetsdkversion="17">
<uses-permission android:name="android.permission.READ_PHONE_STATE">

<application
android:allowbackup="true"
="" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity
android:name="com.example.listencallexample.MainActivity"
="" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">




user1992 18-Nov-13 16:06pm View    
MainActivity.java
--------------------
package com.bitgriff.androidcalls;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
* Main activity, with button to toggle phone calls detection on and off.
*
*/
public class MainActivity extends Activity {

private boolean detectEnabled;

private TextView textViewDetectState;
private Button buttonToggleDetect;
private Button buttonExit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textViewDetectState = (TextView) findViewById(R.id.textViewDetectState);

buttonToggleDetect = (Button) findViewById(R.id.buttonDetectToggle);
buttonToggleDetect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setDetectEnabled(!detectEnabled);
}
});

buttonExit = (Button) findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setDetectEnabled(false);
MainActivity.this.finish();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private void setDetectEnabled(boolean enable) {
detectEnabled = enable;

Intent intent = new Intent(this, CallDetectService.class);
if (enable) {
// start detect service
startService(intent);

buttonToggleDetect.setText("Turn off");
textViewDetectState.setText("Detecting");
}
else {
// stop detect service
stopService(intent);

buttonToggleDetect.setText("Turn on");
textViewDetectState.setText("Not detecting");
}
}

}
CallDetectService.java
-----------------------
package com.bitgriff.androidcalls;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
* Call detect service.
* This service is needed, because MainActivity can lost it's focus,
* and calls will not be detected.
*
*/
public class CallDetectService extends Service {
private CallHelper callHelper;

public CallDetectService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
callHelper = new CallHelper(this);

int res = super.onStartCommand(intent, flags, startId);
callHelper.start();
return res;
}

@Override
public void onDestroy() {
super.onDestroy();

callHelper.stop();
}

@Override
public IBinder onBind(Intent intent) {
// not supporting binding
return null;
}
}
CallHelper.java
----------------
package com.bitgriff.androidcalls;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

/**
* Helper class to detect incoming and outgoing calls.
*
*/
public class CallHelper {

/**
* Listener to detect incoming calls.
*/
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone

Toast.makeText(ctx,
"Incoming: "+incomingNumber,
Toast.LENGTH_LONG).show();
break;
}
}
}

/**
* Broadcast receiver to detect the outgoing calls.
*/
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
Str