How to start something when voice call starts? - android

I have built full voice recorder application.
I would like to start recording when a voice call starts on the phone, how can I detect the Calls state? tried some code and it didn't work for me.
I just need to know hot to start recording when a voice call starts (incoming and outgoing).

Here is an example of what you need.
Declare receiver in AndroidManifest
<receiver android:name=".IncomingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Give read phone state permission in AndroidManifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Create a class IncomingCall with extends BroadcastReceiver class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by matheszabi on Aug/20/2017 0020.
*/
public class IncomingCall extends BroadcastReceiver {
private Context context;
public void onReceive(Context context, Intent intent) {
this.context = context;
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.d("MyPhoneListener",state+" incoming no:"+incomingNumber);
if (state == 1) {
String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.show();
}
}
}
}
Above Android 6.0 you need to handle a bit different the permissions:
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int MY_REQUEST_CODE = 1234;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
MY_REQUEST_CODE);
}
}
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
Toast.makeText(this, "I have access", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "I DON'T have access", Toast.LENGTH_SHORT).show();
}
}
}
}
You must allow the permissions at the first time run:
Here is the screenshot of the working code:

I found how to do so:
package com.example.tsuryohananov.mycallrecorder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by tsuryohananov on 20/08/2017.
*/
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.d("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d("MY_DEBUG_TAG", phoneNumber);
// here i need to save the number for the listview.
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context,"Answered" + phoneNumber, Toast.LENGTH_SHORT).show();
MainActivity.recordStart();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Idle State", Toast.LENGTH_SHORT).show();
MainActivity.stopRecord();
}
}
}
}

Related

Android BroadcastReceiver does not work phone calls

I was looking for examples.
My class look like:
<pre><code>
package com.example.pjimnez.samsung_auto_reply;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
//IntentFilter screenStateFilter;
Button btnStart;
TextView txtHome;
//PhoneState oPhoneState;
IntentFilter filter1;
private final BroadcastReceiver myPhoneState = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state == null) {
//Outgoing call
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//Log.e("tag", "EXTRA_STATE_OFFHOOK");
Toast.makeText(context, "EXTRA_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
//Log.e("tag", "EXTRA_STATE_IDLE");
Toast.makeText(context, "EXTRA_STATE_IDLE", Toast.LENGTH_LONG).show();
} else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
//Log.e("tag", "Incoming number : " + number);
Toast.makeText(context, number, Toast.LENGTH_LONG).show();
} else
Toast.makeText(context, "none", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filter1 = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
registerReceiver(this.myPhoneState, filter1);
btnStart = (Button) findViewById(R.id.btnStart);// Instancia del objeto boton intro
//No funciona en modo escucha
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtHome = (TextView) findViewById(R.id.txtHome);
txtHome.setText("Dio click en el boton!" );//cambio el contenido del TextView
//Envia el mensahe SMS
/*
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5524234613", null, "Mensaje de prueba", null, null);
*/
}
});
}
}
enter code here
</code></pre>
My manifest has permissions to:
android.permission.READ_PHONE_STATE
android.permission.RECEIVE_SMS
android.permission.SEND_SMS
The problem is that my app never show the message on phone change state
i do not know whats is wrong with the BroadcastReceiver
Could you helpme
Try this:
1- add a class PhoneStateBroadcastReceiver which extends BroadcastReceiver and overwrite onReceive()
2- at runtime, request PHONE_STATE permission upfront
ActivityCompat.requestPermissions(myMainActivity,
new String[]{Manifest.permission.READ_PHONE_STATE},
READ_PHONE_STATE_CODE);
and give it via the system dialog
3- make a phone call
You'll see the intent caught in onReceive():
intent: Intent { act=android.intent.action.READ_PHONE_STATE flg=0x10 cmp=com.myApp.network.PhoneStateBroadcastReceiver (has extras) }
Action: android.intent.action.PHONE_STATE
Hope it helps

BroadCast Intent WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION not calling and hence not getting list of peers

Hey I am making an android app based on training samples from developer.android.com Here is my MainActivity Code :
package com.example.hellowifi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final IntentFilter mIntentFilter = new IntentFilter();
private WifiP2pManager mManager = null;
private WifiP2pManager.Channel mChannel = null;
private BroadcastReceiver mReciever;
private ListView mListView;
public static final String TAG = "wifidirectdemo";
// TXT RECORD properties
public static final String TXTRECORD_PROP_AVAILABLE = "available";
public static final String SERVICE_INSTANCE = "_wifidemotest";
public static final String SERVICE_REG_TYPE = "_presence._tcp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReciever = new WiFiBroadCastReciever(mManager, mChannel, this);
startRegistration();
}
#Override
protected void onResume() {
super.onResume();
mReciever = new WiFiBroadCastReciever(mManager, mChannel, this);
registerReceiver(mReciever, mIntentFilter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReciever);
}
public void OnDiscoverButtonClicked(View view) {
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Discovery Process Succeded", Toast.LENGTH_SHORT).show();
System.out.println("changed");
}
#Override
public void onFailure(int reason) {
Toast.makeText(MainActivity.this, "Discovery Process Failed", Toast.LENGTH_SHORT).show();
}
});
}
private static List peers = new ArrayList();/*
private ArrayAdapter listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,peers);*/
static WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
peers.clear();
peers.addAll(peerList.getDeviceList());
}
};
private void startRegistration() {
// Create a string map containing information about your service.
Map record = new HashMap();
record.put(TXTRECORD_PROP_AVAILABLE, "visible");
// Service information. Pass it an instance name, service type
// _protocol._transportlayer , and the map containing
// information other devices will want once they connect to this one.
WifiP2pDnsSdServiceInfo serviceInfo =
WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record);
// Add the local service, sending the service info, network channel,
// and listener that will be used to indicate success or failure of
// the request.
mManager.addLocalService(mChannel, serviceInfo, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Added Local Service", Toast.LENGTH_SHORT);
}
#Override
public void onFailure(int arg0) {
Toast.makeText(MainActivity.this,"Failed to add a service",Toast.LENGTH_SHORT);
}
});
}
}
and my Broadcastreciever class is this :
package com.example.hellowifi;
import java.util.HashMap;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;
import android.widget.Toast;
public class WiFiBroadCastReciever extends BroadcastReceiver {
private MainActivity activity;
private WifiP2pManager mManager=null;
private WifiP2pManager.Channel mChannel=null;
PeerListListener myPeerListListener = null;
public WiFiBroadCastReciever(WifiP2pManager manager, WifiP2pManager.Channel channel,
Activity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.activity = (MainActivity) activity;
}
#Override
public void onReceive(Context context, Intent intent) {
String action=intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
int state=intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);
if (WifiP2pManager.WIFI_P2P_STATE_ENABLED==state)
Log.d("WiFiBroadCastReciever", "WiFi enabled");
else
Log.d("WiFiBroadCastReciever", "WiFi disabled");
}
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
Toast.makeText(activity, "in intent", Toast.LENGTH_LONG).show();
if (mManager != null) {
mManager.requestPeers(mChannel, myPeerListListener);
}
}
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
if (mManager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// We are connected with the other device, request connection
// info to find group owner IP
/*mManager.requestConnectionInfo(mChannel, connectionListener);*/
}
}
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
}
}
final HashMap<String, String> buddies = new HashMap<String, String>();
}
I am getting successful calls to Broadcasting Intents WIFI_P2P_STATE_CHANGED_ACTION,WIFI_P2P_CONNECTION_CHANGED_ACTION,WIFI_P2P_THIS_DEVICE_CHANGED_ACTION but not to WIFI_P2P_PEERS_CHANGED_ACTION
when I am making tethering and portable hotspot to other device and trying to discovering on my device.
Note that: WiFi is enabled and I am also getting Toast of Discovery successful. After that WIFI_P2P_PEERS_CHANGED_ACTION intent is not calling.
I tried searching on each and every thread.
Please try to help me someone. Thanks in advance.
You need to run discoverPeers() on other devices as well.
Now you will receive the WIFI_P2P_PEERS_CHANGED_ACTION intent
I had the same problem before. Please check below scenario how I have fixed the issue.
First I have implemented the same code in server and client android device with minor changes. I have placed a button in server app to invoke the discoverPeers() method. At the same time my client app is running and connected to the same WI-FI network. Then it triggers the WIFI_P2P_PEERS_CHANGED_ACTION event from broadcast in server app. There I have got the list of nearby peers.

How to block call in Android 4.2

My Note :as I have already clarified in my initial post, I don't think it's a duplicate,
I already tried these method and it doesn't work for me,
The code below seems only work for 2.2, it requires MODIFY_PHONE_STATE which is not permitted after Android 2.2****
This is not duplicated questions since i have already looked many other post here and it doesn't work for me
I follow the solution from the link below:
block phone call
TelephonyManager tm = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
But the code give me exception when run on real device(which is Android 4.2)
java.lang.NoSuchMethodException: getITelephony
So, does it still possible use this solution on Android 4.2, if not,does there exist other solutions i can try?
Thanks a lot in advance
Create a file named ITelephony.aidl it should contain these data:
package com.android.internal.telephony;
interface ITelephony
{
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
Create these folders under src
android > internal > telephony
Then Place the ITelephony.adl under telephony folder.
Copy this DeviceStateListener class and place it under any package on your project.
import android.content.Context;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import java.lang.reflect.Method;
public class DeviceStateListener extends PhoneStateListener {
private ITelephony telephonyService;
private Context context;
public DeviceStateListener(Context context) {
this.context = context;
initializeTelephonyService();
}
private void initializeTelephonyService() {
try {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
Class clase = Class.forName(telephonyManager.getClass().getName());
try{
Method method = clase.getDeclaredMethod("getITelephony");
}catch (NoSuchMethodException e){
e.printStackTrace();
}
method.setAccessible(true);
telephonyService = (ITelephony) method.invoke(telephonyManager);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onCallStateChanged(int state, final String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
boolean isNumberIsBlocked=false;
// You can check here if incomingNumber string is under your blacklisted numbers
if (isNumberIsBlocked) {
try {
// This is the main code that block the incoming call.
telephonyService.endCall();
Thread t = new Thread(new Runnable() {
#Override
public void run() {
// You can run anything here lets say a notice to the user if a call is blocked
}
});
t.start();
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
}
}
}
Here is another important class "ServiceReceiver" place it also under any package of your project and resolve all possible imports.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class ServiceReciever extends BroadcastReceiver
{
private static TelephonyManager telephony;
private static DeviceStateListener phoneListener;
private static boolean firstTime=true;
public ServiceReciever(Context context)
{
telephony=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
phoneListener=new DeviceStateListener(context);
}
#Override
public void onReceive(Context context, Intent intent)
{
if(firstTime)
{
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
firstTime = false;
}
}
// You can use this in the future to stop the call blocker feature.
public void stopListening() {
telephony.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
firstTime=true;
}
}
Copy this CallBlockerService class also and place it under any package of your project. It is an unkillable service that invokes the ServiceReceiver class.
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import com.katadigital.phone.callsmsblocker.callListener.ServiceReciever;
public class CallBlockerService extends Service {
public static final int notification_id = 111;
// ---------------------------------------
// Listening Services
// ---------------------------------------
private static ServiceReciever service;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
service = new ServiceReciever(getApplicationContext());
registerReceiver(service, new IntentFilter(
"android.intent.action.PHONE_STATE"));
System.out.println("Call blocker is running now");
}
#Override
public void onDestroy() {
service.stopListening();
unregisterReceiver(service);
service = null;
cancelStatusBarNotification();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public void cancelStatusBarNotification() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(notification_id);
}
}
Place this AfterBootReceiver class beside our CallBlockerService. Its job is to restart the blocker service when the phone starts from shutdown.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AfterBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Intent serviceLauncher = new Intent(context, CallBlockerService.class);
context.startService(serviceLauncher);
}
}
Lastly place this on your AndroidManifest under tag.
<receiver android:name="com.callblocker.services.AfterBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name="com.callblocker.services.CallBlockerService" >
</service>
Replace "com.callblocker.services" with the folder location of the CallBlockerService and your AfterBootReceiver
I have tested this code until Android 4.4 KitKat. I hope you can follow the steps and it helps you with your problem.

Android Dialer in the Background

Is it possible to call the Intent.ACTION_CALL in the background ?
I want my application to call but I don't want it to put in the background, I want it to stay in the foreground while it is calling.
The Intent.ACTION_CALL will call the default Android Phone activity and call the number you provided. If you want your App to be in the foreground, which i take to understanding you are builder your own calling app, you will have to implement the Calling part yourself.
Otherwise you can launch your activity with say a 5 seconds delay if you just want to show the user a context of your application.
You can invoke your activity after making a call using this,
package com.sdi.androidcall;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button call;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (Button) findViewById(R.id.call);
final PhoneCallListener phoneListener = new PhoneCallListener();
final TelephonyManager telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}, 10000);
}
});
}
private class PhoneCallListener extends PhoneStateListener {
boolean flag = true;
String LOG_TAG = "Call TEST";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "number: " + incomingNumber);
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
Log.i(LOG_TAG, "OFFHOOK");
// invoking activity
if (flag) {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
System.out.println("flagged retrieving app");
flag = false;
}
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
Log.i(LOG_TAG, "IDLE");
Log.i(LOG_TAG, "restart app");
}
}
}
}
The activity will be called after a delay of 10 seconds here using PhoneStateListener
You need to specify the intent permissions in the mainfest as
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Try it, let me know if worked.

ACTION_UMS_CONNECTED not getting flag

i got some problem with checkout weather device is connected with PC or not? so i have found that intent action ACTION_UMS_CONNECTED is used to get flag to check weather device is connected with PC or not.
i have tried one example but i cant able to get any flag.... I have past code here
package com.Mediamount;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class Mediamount extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter();
this.registerReceiver(mIntentReceiver, filter);
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean queryRestart = false;
if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
Toast.makeText(getBaseContext(), "asdasd", Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
}
}
};
}
Is there anything are missing in manifest file so please tell me.
Update:
package com.Mediamount;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class Mediamount extends Activity {
public static BroadcastReceiver mReceiver1 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED);
// install an intent filter to receive SD card related events.
IntentFilter intentFilter1 = new IntentFilter
(Intent.ACTION_MEDIA_MOUNTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter1.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter1.addAction(Intent.ACTION_MEDIA_EJECT);
// install an intent filter to receive UMS(USB) related events.
intentFilter1.addAction(Intent.ACTION_UMS_CONNECTED);
intentFilter1.addAction(Intent.ACTION_UMS_DISCONNECTED);
intentFilter1.addAction("USB_INTENT");
intentFilter1.addDataScheme("file");
mReceiver1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, context.toString(),Toast.LENGTH_LONG).show();
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
//Toast.makeText(context, "SD Card mounted",Toast.LENGTH_LONG).show();
} else if (action.equals
(Intent.ACTION_MEDIA_UNMOUNTED)) {
// Toast.makeText(context, "SD Card unmounted",Toast.LENGTH_LONG).show();
} else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
// Toast.makeText(context, "SD Card scanner started",Toast.LENGTH_LONG).show();
//System.out.println("SD Card scanner started");
} else if (action.equals
(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
// Toast.makeText(context, "SD Card scanner finished",Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
// Toast.makeText(context, "SD Card eject",Toast.LENGTH_LONG).show();
} else if(action.equals(Intent.ACTION_UMS_CONNECTED))
{
// Toast.makeText(context, "connected",Toast.LENGTH_LONG).show();
} else if(action.equals
(Intent.ACTION_UMS_DISCONNECTED)) {
// Toast.makeText(context, "disconnected",Toast.LENGTH_LONG).show();
}
// BONG_TEST }
}
};
registerReceiver(mReceiver1, intentFilter1);
}}

Categories

Resources