Terminate Incoming Call Programmatically Android - android

I need to programmatically end an Incoming Call when a button from my App is being clicked.
If I use TelephonyService, it is working fine below 4.0 versions. But it is not working in 4.2.2 version.

TelephonyManager telephony = (TelephonyManager) srvs
.getSystemService(Context.TELEPHONY_SERVICE);
Class c;
final com.android.internal.telephony.ITelephony telephonyService;
try {
c = Class.forName("android.telephony.TelephonyManager");//telephony.getClass().getName());
Log.i("TelephonyClass Name", telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
TimerTask task = new TimerTask() {
#Override
public void run() {
try {
if (telephonyService.isIdle()
|| telephonyService.isOffhook()
|| telephonyService.isRinging())
telephonyService.endCall();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
long delay = Integer.parseInt(cutofftime) * 1000;
new Timer().schedule(task, delay);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Related

auto reject is not working,Now,how to automatically unregister the broadcast receiver

hello i am creating three different type of modules for mobile where the user can choose any of them by clicking a button.Now each module has its set of option to accept or reject calls.When for a particular module user selects reject then i am storing a Boolean value in database.
I want that whenever an user has selected call reject and checking out of that module to select another module then that call reject functionality should automatically stop.
Now,how to automatically unregister the broadcast receiver that rejects call.
CallBlocker = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//Java Reflections
Class c = null;
try {
c = Class.forName(telephonyManager.getClass().getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getITelephony");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.setAccessible(true);
try {
telephonyService = (ITelephony) m.invoke(telephonyManager);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
}//onReceive()
PhoneStateListener callBlockListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
if (autoreject==1) {
try {
telephonyService.endCall();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
unregisterReceiver(CallBlocker);
}
}
}
};
};/
I think this could help you out:-
How to reject/close specific incoming number
How to reject a call programatically android
https://androiddesk.wordpress.com/2012/08/02/blocking-a-call-without-user-intervention-in-android/#comments

I wanna play recording again in existing file

I am a newbie.
I'm developing an android (4.2) app to record some audio.
The code seems to run fine the first time, and it even saves the files on the SD Card.
But, if I try to record again, my app crashes.
This is my code:
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(StringVoiceFile);
this is my function
public void start(){
String PathImagePage = AppDirectory+"/content/"+userid+"/books/"+book_id+"/";
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
File VoiceFile = new File(PathImagePage, +"-"+pagenumber+".mp3");
MediaPlayer m = new MediaPlayer();
if (VoiceFile.exists()){
try {
myAudioRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.setDataSource(StringVoiceFile);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myAudioRecorder.start();
} catch (IllegalStateException e) {
Log.e(TAG, "start() failed = " + e );
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setEnabled(true);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start_again));
} else {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
} catch (IOException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start));
}
}
Please help me to solve this issue

Android Emulator crashing when saving an reading files

I'm trying to save a custom object when I click a menu item. The problem is its crashing. Before we start, when I try to read that data in another activity, that also crashes. Here is my code where I'm trying to save the data:
Here's a pastebin link.
my write function in the menu
myInfo.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
Intent ourIntent = new Intent(Results.this, listTimes.class);
ourIntent.putExtra("Meeting", meetingObj);
try {
fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(businesses.get(positionChecked));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//startActivity(ourIntent);
return true;
}
// My read function
private void getData()
{
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
business = (Business) ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Playing Audio file

I use the following code to play audio from sdcard. But it doesn't play, even doesn't throw error. My file is in the following path /sdcard/media/blueeye.mp3. Any suggestions.
public void audioPlayer(){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(mp.setDataSource(getExternalFilesDir() + "/media/blueeye.mp3"); );
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
I think it should be /mnt/sdcard/media/blueeye.mp3 and please use getExternalFilesDir():
mp.setDataSource(getExternalFilesDir() + "/media/blueeye.mp3");
Also, If you'd set your own onErrorListener, you will be able to see easily what the problem is.

Android - public method setMobileDataEnabled in ConnectivityManager not available in SDK

i'm trying to use the method setMobileDataEnabled of the ConnectivityManager class, using SDK 2.2.
According to http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source this method is declared public but with #hide is not available in the SDK and in Eclipse.
In order to bypass the hiding I wrote the following function to toggle the mobile data connection on/off.
public void setMobileData(boolean toBeEnabled){
Object myObj= getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) myObj;
Class c = null;
try {
c = Class.forName(cm.getClass().getName());
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getMobileDataEnabled");
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (NoSuchMethodException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Object mobileDataEnabled=null;
if (m!=null){
m.setAccessible(true);
Type res_of_m= m.getGenericReturnType();
Type[] pars_of_m= m.getGenericParameterTypes();
try {
mobileDataEnabled = (m.invoke(cm));
if (mobileDataEnabled!=null)
if (mobileDataEnabled.equals(!toBeEnabled)){
Method m2 = null;
try {
int index=0;
boolean method_found=false;
Method[] available_methods= c.getDeclaredMethods();
for (Method method : available_methods) {
// following line doesn't work
// method.getName()=="setMobileDataEnabled"
if (method.getName().contains("setMobileDataEnabled"))
{
method_found=true;
}
if (method_found==false)
index++;
}
// following line doesn't work
//m2 = c.getDeclaredMethod("setMobileDataEnabled");
m2 = (c.getDeclaredMethods())[index];
if (m2!=null){
m2.setAccessible(true);
m2.invoke(cm,toBeEnabled);
}
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InvocationTargetException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To make it working I also added the android.permission.WRITE_SECURE_SETTINGS" in the manifest and installed in /system/app according to Android: Add app to firmware, use WRITE_SECURE_SETTINGS.
Does anyone know a better way?
try {
final ConnectivityManager cm = (ConnectivityManager) this.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class connectivityManager = Class.forName(cm.getClass()
.getName());
final Method[] methods = connectivityManager.getDeclaredMethods();
for (final Method method : methods) {
if (MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
.equals(method.getName())) {
method.invoke(cm, true);
}
}
} catch (final ClassNotFoundException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalArgumentException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalAccessException e) {
Log.e("Class", e.getMessage());
} catch (final InvocationTargetException e) {
Log.e("Class", e.getMessage());
}

Categories

Resources