Network Data Packet connectivity intent - android

I am writing an Android application which can enable and disable the Network Data packet connection. I am also using one broadcast receiver to check the Network Data packet connection. I have registered broadcast receiver and provided required permission in Manifest file. But when I run this application it changes the connection state and after that it crashes. But when I don't include this broadcast receiver it works fine. I am not able to see any kind of log which can provide some clue.
Here is my code for broadcast receiver.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.simplewidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- Permissions -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SimpleWidgetExampleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
<receiver
android:name=".ExampleAppWidgetProvider"
android:label="Widget ErrorBuster" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget1_info" />
</receiver>
-->
<receiver android:name=".ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
My Broadcast receiver class is as following.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(info.getType() == ConnectivityManager.TYPE_MOBILE){
if(info.isConnectedOrConnecting()){
Log.e("RK","Mobile data is connected");
}else{
Log.e("RK","Mobile data is disconnected");
}
}
}
}
my Main activity file.
package com.rakesh.simplewidget;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SimpleWidgetExampleActivity extends Activity {
private Button btNetworkSetting;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btNetworkSetting = (Button)findViewById(R.id.btNetworkSetting);
if(checkConnectivityState(getApplicationContext())){
btNetworkSetting.setBackgroundColor(Color.GREEN);
}else{
btNetworkSetting.setBackgroundColor(Color.GRAY);
}
}
public void openNetworkSetting(View view){
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
Context context = view.getContext();
boolean enabled = !checkConnectivityState(context);
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try{
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
if(enabled){
Toast.makeText(view.getContext(), "Enabled Network Data", Toast.LENGTH_LONG).show();
view.setBackgroundColor(Color.GREEN);
}
else{
Toast.makeText(view.getContext(), "Disabled Network Data", Toast.LENGTH_LONG).show();
view.setBackgroundColor(Color.LTGRAY);
}
}catch(Exception e){
Log.e("Error", "some error");
Toast.makeText(view.getContext(), "It didn't work", Toast.LENGTH_LONG).show();
}
}
private boolean checkConnectivityState(Context context){
final TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
ConnectivityManager af ;
return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
}
}
Log file:
java.lang.RuntimeException: Unable to instantiate receiver com.rakesh.simplewidget.ConnectivityReceiver: java.lang.ClassNotFoundException: com.rakesh.simplewidget.ConnectivityReceiver in loader dalvik.system.PathClassLoader[/data/app/com.rakesh.simplewidget-2.apk]
E/AndroidRuntime(26094): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1777)
E/AndroidRuntime(26094): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
E/AndroidRuntime(26094): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
E/AndroidRuntime(26094): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(26094): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(26094): at android.app.ActivityThread.main(ActivityThread.java:3691)
E/AndroidRuntime(26094): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(26094): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(26094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
E/AndroidRuntime(26094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
E/AndroidRuntime(26094): at dalvik.system.NativeStart.main(Native Method)
It seems Android is not able to recognize file Broadcast Receiver class. Any idea why I am getting this error?
PS: Some information about Android environment and platform.
- Android API 10.
- Running on Samsung Galaxy II which has android 2.3.6
Edit:
my broadcast receiver file ConnectivityReceiver.java was present in default package and it was not being recognized by Android. Android was looking for this file in current package i.e com.rakesh.simplewidget; I just moved connectivityReciever.java file to com.rakesh.simplewidget package and problem was solved.

My Broadcast receiver file ConnectivityReceiver.java was present in default package by mistake. And Android was not able to recognized this file because it was searching ConnectivityReceiver in the current package. i.e com.rakesh.simplewidget;
It was simple unnoticeable mistake which caused me half an hour to debug this.
Thank you guys for taking time reading my question and providing your comments.
Updated the main post.

Related

Reading Array/Payload on NFC Tag?

I've edited my Manifest code and my Activity code again to reflect appropriate intent filter requirements. However, I'm still getting the same results. I'm unable to read my external record, and I don't know why? When I scan my tags with the NFC Tools app, I can see everything as I would expect. My code produces a NullPointer when it attempts to "read" my array and I have no idea why?
--------------BEGIN MANIFEST--------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bmt_01">
<uses-permission android:name="android.permission.NFC"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk android:minSdkVersion="15"/> ///must be changed in build.gradle.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="vnd.android.nfc"
android:host="ext"
android:pathPrefix="/com.example.bmt_01:externaltype"
/>
</intent-filter>
</activity>
</application>
</manifest>
--------------END MANIFEST--------------------
--------------BEGIN MAIN ACTIVITY-------------
package com.example.bmt_01;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
String currentPayload = "";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onNewIntent(Intent intent)
{
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
//added the below lines, can delete if they don't work.
NdefRecord payload = ((NdefMessage)rawMsgs[0]).getRecords()[0];
String currentPayload = new String(payload.getPayload());
if (currentPayload.equals("1"))
{
Toast.makeText(this, "NFC Scan1: " + currentPayload, Toast.LENGTH_SHORT).show();
String url = "http://www.google.com";
Intent l = new Intent(Intent.ACTION_VIEW);
l.setData(Uri.parse(url));
startActivity(l);
}
///else
///{
if (currentPayload.equals("2"))
{
Toast.makeText(this, "NFC Scan2: " + currentPayload, Toast.LENGTH_SHORT).show();
String url = "http://www.yahoo.com";
Intent l = new Intent(Intent.ACTION_VIEW);
l.setData(Uri.parse(url));
startActivity(l);
}
///}
}
protected void onResume()
{
super.onResume();
Toast.makeText(this, "NFC Scan3: ", Toast.LENGTH_SHORT).show();
enableForegroundDispatchSystem();
}
#Override
protected void onPause()
{
super.onPause();
disableForegroundDispatchSystem();
}
private void enableForegroundDispatchSystem() {
Toast.makeText(this, "NFC Scan4: ", Toast.LENGTH_SHORT).show();
}
private void disableForegroundDispatchSystem() {
Toast.makeText(this, "NFC Scan5: ", Toast.LENGTH_SHORT).show();
onNewIntent(getIntent());
}
}
--------------END MAIN ACTIVITY---------------
UPDATE*
I should add that my AAR functions correctly. When I have the above app installed on my phone, but do not have it open, my AAR automatically opens and launches the application when I scan my test tag. So, I'm able to "see" the AAR in the array, but NOT the external record.
I'm still unable to read the external record on my tag. Obviously I'm doing something wrong, but I have no idea.
You mix togerthe message and record in your for loop. However there is only one NDEF message, but this message can hold several records. You must use only rawMsgs[0]. Then your for loop can iterate over all records of that message.
I've created a library which lets you parse NDEF records.
You're on the right track - wrapping binary paylod with using the external type - the library supports plugging in your own external type parser/record type(s), if you so prefer.

Broadcast receiver for incoming call not working?

I am trying to display toast on incoming call received. but i am not getting any display. I have mention the receiver inside the manifest file which also contains permission required for phone calls.
the following is code that i have used.
// inside IncomingCall broadcastreceiver
package com.example.shailesh.callbroadcastreceiver;
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;
public class IncomingCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}
}
And i have specify in menifest file as follows :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingCall" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
what else i have to include in order to get toast display on incoming call received.
i Have also included following code in my MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(getApplicationContext(),IncomingCall.class);
startService(i);
}
}
is class IncomingCall a service ? if not then why you are firing an intent to start service.

Constantly check for wireless network change [duplicate]

This question already has an answer here:
How can I monitor the network connection status in Android?
(1 answer)
Closed 10 years ago.
I would like to constantly check whether the phone is connected to a specific wireless network. I thought of a service and the SSID of the network of course, but how?
You'll want
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
In your receiver tag.
Or if you want more control over it, before registering BroadcastReceiver set these up:
final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(yourReceiver, filters);
WIFI_STATE_CHANGED
Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.
STATE_CHANGE
Broadcast intent action indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String
Also, you'll need to specify the right permissions:
<user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Whole Source Code: Download
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.temp.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>
<receiver android:name="com.example.temp.MyWiFiStateListener">
<intent-filter >
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
</application>
MainActivity.java
package com.example.temp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Broadcast Receiver:
package com.example.temp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
public class MyWiFiStateListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Log.d("TEMP", action);
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(info.getType() == ConnectivityManager.TYPE_WIFI){
WifiManager myWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
Log.d("TEMP","BSSID :: " + wifiInfo.getBSSID() + "SSID :: " + wifiInfo.getSSID());
}
}
}
}

Detecting phone activity in a class

So in my Android app, I've been testing how to detect an incoming and outgoing call. I got it to work by building a class that extends a BroadcastReceiver, but in that class if I call another class it crashes. For example MyClass mc = new MyClass(); mc.functionname();
My actual app is running a looping sound clip. I want to pause that sound clip when an incoming or outgoing call is made. The soundclip is played in a class that extends Activity. How do I do this?
This is what I have for the BroadcastReceiver class and my manifest.
package com.anthony.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class inComingReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("MYTAG",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("MYTAG","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("MYTAG",":) Incomng Number: " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.anthony.phone"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".CallTActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.anthony.phone.inComingReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
i had also this kind of problem. i solved by calling activity from broadcast receiver And writing my class code in particular Activity. As mentioned below
Intent i = new Intent(context,newActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
i hope it will help you to solve your query.
ask me if you further have any doubt.

Broadcast receiver not working

I am trying to test Broadcast receiver for outgoing calls. Please help.
Here is my CallCounter class:
package com.callout;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CallCounter extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("Tony+++++++++++","Outgoing Call finished");
// Call Finished -> stop counter and store it.
// callStop=new Date().getTime();
Context c = getApplicationContext();
c.stopService(new Intent(c,ListenerContainer.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("++++++++++++++++Tony","Outgoing Call Starting");
// Call Started -> start counter.
// This is not precise, because it starts when calling,
// we can correct it later reading from call log
// callStart=new Date().getTime();
break;
}
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return this.getApplicationContext();
}
}
Here is myReceiver
package com.callout;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class myReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
Log.d("T+++++++++++++++++ony","In mYRecieverrr");
context.startService(new Intent(context,ListenerContainer.class));
}
}
}
Here is myService:
package com.callout;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ListenerContainer extends Service {
public class LocalBinder extends Binder {
ListenerContainer getService() {
Log.d("Tony","OKKK HEREEEEEEEEEEEEEeEEEEE");
return ListenerContainer.this;
}
}
#Override
public void onStart(Intent intent, int startId) {
TelephonyManager tManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
CallCounter callCounter=new CallCounter();
tManager.listen(callCounter,PhoneStateListener.LISTEN_CALL_STATE);
Log.d("To++++++++++++++ny","Call COUNTER Registered");
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
}
And this is the manifest file if i m missing some permission:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callout"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".CallCounter"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".myReceiver"
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:enabled="true" android:name=".ListenerContainer" />
<receiver android:name=".myReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
Please point out my mistake.
You must add the following permission to your manifest since your are registering a phonestatelistener.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
It looks like your receiver is missing an attribute in the manifest file :
http://developer.android.com/guide/topics/manifest/receiver-element.html
Look at the exported attribute, it should be set tot true to receive broadcast from other apps.
Regards,
Stéphane
There's a working solution with full source here that meets your requirements
https://www.codeproject.com/articles/548416/detecting-incoming-and-outgoing-phone-calls-on-and

Categories

Resources