Codes for call recording in android - android

I am making and application that records calls. Here is my code to record a call and save the file in the SD card under songs folder. But the problem is that this code was working fine but later it is not working. I cannot find what is the problem. Can you please help me out?
My Broad cast receiver:
public class BrCallReceive extends BroadcastReceiver {
#Override
public void onReceive(Context c, Intent i) {
Bundle extras = i.getExtras();
Intent x = new Intent (c, EavesDropperActivity.class);
x.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Log.w("DEBUG", "MATCHES");
Toast.makeText(c, "Launching record APP !", Toast.LENGTH_LONG).show();
c.startActivity(x);
}
}
}
}
My Recording activity:
public class EavesDropperActivity extends Activity {
/** Called when the activity is first created. */
MediaRecorder m_recorder = new MediaRecorder();
TelephonyManager t_manager ;
PhoneStateListener p_listener ;
String record_state;
Uri file;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Toast.makeText(getBaseContext(),
"Executing Activity",
Toast.LENGTH_LONG).show();
t_manager = (TelephonyManager) getSystemService (Context.TELEPHONY_SERVICE);
p_listener = new PhoneStateListener() {
#Override
public void onCallStateChanged (int state, String incomingNumber) {
switch (state) {
case (TelephonyManager.CALL_STATE_IDLE) :
stop_recorder();
//t_manager.listen(p_listener, PhoneStateListener.LISTEN_NONE);
//finish();
break;
case (TelephonyManager.CALL_STATE_OFFHOOK) :
start_recorder();
break;
case (TelephonyManager.CALL_STATE_RINGING) :
break;
}
}
};
t_manager.listen(p_listener, PhoneStateListener.LISTEN_CALL_STATE);
return;
}
public void start_recorder () {
m_recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
m_recorder.setOutputFormat(OutputFormat.THREE_GPP);
m_recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/songs/audionew.3gpp");
m_recorder.setAudioEncoder(AudioEncoder.AMR_NB);
try {
m_recorder.prepare();
m_recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop_recorder () {
m_recorder.stop();
m_recorder.release();
Uri file = Uri.fromFile(
new File(Environment.getExternalStorageDirectory(),"/songs/audionew.3gpp"));
Toast.makeText(getBaseContext(),
"record stored at " + file.toString(),
Toast.LENGTH_LONG).show();
t_manager.listen(p_listener, PhoneStateListener.LISTEN_NONE);
finish();
}
}
My manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testapp.EavesDropperActivity"
android:label="#string/app_name" >
</activity>
<receiver android:name="BrCallReceive" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>

I think the broadcast receiver is not getting activated wile i am calling my phone.
How do you know your broadcast receiver is not working? Place a Log message inside onReceive() right at the beginning. It might be that your extras == null
But how to make my receiver functional now?
Check out example #5 and just follow the same steps.
Bro i dont think activity will get launched.
Well, it does: You are starting it in your onReceive():
Intent x = new Intent (c, EavesDropperActivity.class);
c.startActivity(x);
However, you are not setting any content in your Activity i.e no UI screen is presented because you have this line commented in onCreate() of EavesDropperActivity Activity:
//setContentView(R.layout.main);
So, you need to think hard on what you are trying to achieve in EavesDropperActivity
HTH.

Put this
<android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
in mainfest file for broadcast receiver.

Add your package name to your receiver class as,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.testapp.EavesDropperActivity"
android:label="#string/app_name" >
</activity>
<receiver android:name="com.example.testapp.BrCallReceive" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>

Related

Broadcast Receiver does not deduct calls - Android

I am trying to do some work at incoming call times. I am trying like this
public class callDeductor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}
This is my Manifeast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deduct.calldeduct"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="Call Deduct" >
<receiver android:name="com.deduct.calldeduct.callDeductor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
I check with mobile and emulator. But it does not show any toast message. Please let me know where I did mistake.
I got reference from this websites.
Link 1
Link 2
Continuing my conversation with asker as an answer.I Just started a new Android application and worked out like i have said in the comments.. The manifest looks like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dj.randomtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<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" >
<receiver android:name=".CallDetector">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The CallDetector class looks like this(ur code):
public class CallDetector extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}

Security Exception: Not allowed to start a service Intent

Ok i am new to android development so please bear with me here. I am following the learning android book and have this problem where the refresh service wont work as it doesnt have the necessary permission. Can anyone tell me what is causing the issue? The logcat throws up runtime error: *java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.yamba/.RefreshService } without permission com.example.yamba.permission.REFRESH at com.example.yamba.TimelineActivity.onOptionsItemSelected(TimelineActivity.java:138)
*
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yamba"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.example.yamba.permission.REFRESH" />
<application
android:name=".YambaApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.yamba.StatusActivity"
android:configChanges="orientation"
android:icon="#drawable/ic_launcher"
android:label="#string/status_Update" >
</activity>
<activity
android:name=".TimelineActivity"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".UpdaterService" >
</service>
<service
android:name=".RefreshService"
android:permission="com.example.yamba.permission.REFRESH" >
<intent-filter>
<action android:name="com.example.yamba.RefreshService" />
</intent-filter>
</service>
<activity
android:name=".PrefsActivity"
android:label="#string/Preferences" >
</activity>
<receiver android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.yamba.REFRESH_ALARM" />
</intent-filter>
</receiver>
</application>
package com.example.yamba;
import java.util.List;
import winterwell.jtwitter.Twitter.Status;
import winterwell.jtwitter.TwitterException;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class RefreshService extends IntentService {
static final String TAG = "RefreshService";
public RefreshService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
((YambaApp) getApplication()).pullAndInsert();
Log.d(TAG, "onHandleIntent");
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "OnCreated");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "OnDestroy");
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Intent intentUpdater = new Intent(this, UpdaterService.class);
Intent intentRefresh = new Intent(this, RefreshService.class);
Intent intentPrefs = new Intent(this, PrefsActivity.class);
Intent intentTimeline = new Intent(this, StatusActivity.class);
switch (item.getItemId()) {
case R.id.item_start_service:
startService(intentUpdater);
return true;
case R.id.item_stop_service:
stopService(intentUpdater);
return true;
case R.id.item_refresh:
startService(intentRefresh);
return true;
case R.id.item_prefs:
startActivity(intentPrefs);
return true;
case R.id.item_status_update:
startActivity(intentTimeline);
default:
return false;
}
The onOptionsItemSelected is used to call the refresh service.
Really Appreciate the help. Thanks!!
you need to declare permission in your manifest
<permission
android:name="com.example.yamba.permission.REFRESH"
android:protectionLevel="signature"
/>
You need to declare this REFRESH Permission in the manifest as well.
here is an example code of how to declare a permission
<permission
android:name="A_PERMISSION"
android:description="#string/broadcast_permission_desc"
android:label="#string/broadcast_permission_label"
android:permissionGroup="#string/broadcast_permission_group"
android:protectionLevel="signature" />
For Reference See this Page

SipDemo's onReceive() SipAudioCall NullPointerException give

I am Creating incoming call Receive activity by SipDemo . My activity has two button "accept call" and "reject call". When i got incoming call then my own activity is open such as
----> incoming call class ----> onReceive()----> go to my call pick activity.
here is this above two button such as accept and reject call. but when i hit on "accept call" . A Nullpointer Exception generate. it is on SipAudiocall incoming;
this is incoming class:-
static SipAudioCall incomingCall = null;
#Override
public void onReceive(final Context context, Intent intent) {
Intent startActivity = new Intent();
startActivity.setClass(context, Mycall.class);
startActivity.setAction(IncomingCallReceiver.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(startActivity);
}
public static void acceptCall() {
incomingCall.sendDtmf(9);
try {
incomingCall.sendDtmf(9);
incomingCall.answerCall(200);
//wtActivity.gototimer("i");
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// wtActivity.gototimer("i");
}
public void rejectCaLL() {
try {
incomingCall.endCall();
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is my call pick activity class:-
public class Mycall extends Activity {
Button acct;
IncomingCallReceiver incomingCallReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
// Note that none of the preferences are actually defined here.
// They're all in the XML file res/xml/preferences.xml.
super.onCreate(savedInstanceState);
setContentView(R.layout.my);
acct = (Button) findViewById(R.id.button1);
acct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IncomingCallReceiver.acceptCall();
}
});
}
}
This is AndroidManifest.xml :-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.sip">
<application android:icon="#drawable/icon" android:label="SipDemo">
<service android:name=".MService">
</service>
<activity android:name=".WalkieTalkieActivity"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SipSettings" android:label="set_preferences"/>
<receiver android:name=".IncomingCallReceiver" android:enabled="true" android:label="Call Receiver">
<intent-filter>
<action android:name="android.SipDemo.INCOMING_CALL" />
</intent-filter>
</receiver>
<activity android:name=".Mycall"
>
</activity>
<activity android:name=".Myclass"/>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
Please Expert Help me. I has involved this problem since 1 month.
I am wait for your response.
Thank you
Becouse your
IncomingCallReceiver incomingCallReceiver;
is null at time of calling it...
If you create a static class you must take care to initiate it maybe in creator-method.
create an abstract class:
public abstract class IncomingCallReceiver
and change your method to public static final

by pressing back button i want to reject android call

I have a BroadcastReceiver and I know how to reject calls. But I need help rejecting a call when I press the back button, because I have some sort of error in my code...
public class CallReceiveD extends BroadcastReceiver {
private ITelephony telephonyService;
public String phoneNumber;
public Bundle extras;
public TelephonyManager tm;
public Intent i;
#Override
public void onReceive(Context context, Intent intent) {
i=intent;
extras = intent.getExtras();
if (extras != null)
{
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast toast= Toast.makeText(context,phoneNumber, Toast.LENGTH_LONG);toast.show();
tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
// telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//#Override
public void onBackPressed() {
telephonyService.endCall();
return;
}
}
My manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="denza12Des.call"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".Call2BackActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".CallReceiveD">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
as far as I know you cant use the backbutton onreceive event that u are using to decline call, but the back button press function i ok and it should work if you use it other than in the broadcast receive onReceive

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.

Categories

Resources