Android broadcast receiver listening outgoing call - android

I am new to android. I am working on broadcastreceiver. I want to create a receiver listening outgoing call. What I expected is when ever out going call is made write logcat " It is Ok" .
But it show message on log as "unexpected value from nativegetenabledtags".
Following is my manifestFile.
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="de.vogella.android.receiver.phone.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="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
Following is receiver Class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("MY_DEBUG_TAG", phoneNumber);
}
}
}
}
Please give me some solution.

I'm not sure if this is the exact reason but for your class name you need to put a dot (.) in the front. Like below.
<receiver android:name=".MyPhoneReceiver" >
I'm not sure if this is your exact issue but without the . will cause you other issues if this isn't the reason

Check this answer: Unexpected value from nativeGetEnabledTags: 0
Add this filter to the LogCat: ^(?!.(nativeGetEnabledTags)).$
It is a bug introduced in the latest revision of the tools... Google is working on a fix on the next version

Related

Android service does not start all the times after reboot

I want to start a service after reboot. The problem I have is that this does not happen every time (at least the first 20 minnutes). I have study many questions into stackoverflow and try a number of the provided solutions however sometimes the service does not automaticaly start after reboot.
Also I have to add another parameter this of the foreground service for android versions O and above.
Could someone give me any advice?
AndroidManifest.xml
.... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
>
<activity
android:name=".Activities.MainActivity"
....
</activity>
<activity
...
</activity>
<receiver android:name=".Helpers.BootCompletedIntentReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- I think that this is also not necessery <category android:name="android.intent.category.DEFAULT" /> -->
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="true"></service>
</application>
BroadcastReciever
package com.abc.Helpers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.abx.Activities.MainActivity;
import com.abc.Services.myService;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, myService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context,serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
I also found in a comment here that reciever should be registered into an activity of the application and this is the code in Mainactivity. Do you agree?
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootCompletedIntentReceiver.class.getName());
if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
..
}

Launch Activity in android via Dialpad using Secret Code

I just want to launch activity via Secret Code .
Although , there are many solutions available for this problem but none of them worked for me. It seems like BroadcastReceiver is not listening .
I am using Mi4i MIUI 7.
Here's my code. Please Help!!
MainActivity.java
package com.example.jatin.myapplication2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have even tried the below commented code , but that didn't work.
MySecretCodeReceiver.java
package com.example.jatin.myapplication2;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Jatin on 05-Aug-16.
*/
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String uri = intent.getDataString();
String sep[] = uri.split("://");
if (sep[1].equalsIgnoreCase("1234"))
{
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.jatin.myapplication2.MainActivity");
context.startActivity(launchIntent);
}
else if (sep[1].equalsIgnoreCase("5678")) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("net.one97.paytm");
context.startActivity(launchIntent);
}
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// setResultData(null);
// context.startActivity(i);
}
}
}
I have also tried putting android:host="1234" in manifest ,still not getting desired results
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
<receiver android:name="receivers.MySecretCodeReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Got it ....
We have to register our MySecretCodeReceiver as a <receiver> within the <application> element in the AndroidManifest.xml file.
And , Its Done. :)

Google Glass GDK , unable to register BroadcastReceiver , onReceive() not called

Im trying to write a simple test app that listens in the background(using BroadcastReceiver) for Google Glass camera events such as taken picture, and than take that picture & do something with it. onReceive() is not being called.
My Google Glass is running XE19.1 OS & my GDK api is Google Glass Development Preview 4.4.2.
My class that extends BroadcastReceiver:
package com.example.recentpicscollage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.widget.Toast;
public class TestReciever extends BroadcastReceiver {
//Attempt # 1 listen to picture taken events
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("DEBUG", "RecentPicsCollage BroadcastReceiver");
//use the Cursor interface to query data from the Intent ( Intents asynchronous intera & inter app communication)
Cursor cursor = context.getContentResolver().query(intent.getData(),null,null, null, null);
//The moveToFirst() method is used to position the cursor pointer at the beginning of the data set
cursor.moveToFirst();
//get data under the column _data
String image_path = cursor.getString(cursor.getColumnIndex("_data"));
//as toast
Toast.makeText(context, "image data : " + image_path, 1000).show();
//as log
Log.d("DEBUG", "image data: "+image_path);
for (int i=0; i<cursor.getColumnNames().length; i++)
{
Log.d("DEBUG", "column name: "+cursor.getColumnNames()[i]);
}
//just print out all data to see what data is available
while (cursor.isAfterLast()!=true)
{
//move to next row
cursor.moveToNext();
//get data
String data = cursor.getString(cursor.getColumnIndex("_data"));
Log.d("DEBUG", "image data: "+data);
}
}
/*
//Attempt # 2 , Test to see if i can capture any other action such as TAP
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("onReceive TAP");
Log.d("DEBUG", "onReceive TAP");
}
*/
}//end class
AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recentpicscollage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<!-- permission to read SD card -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.recentpicscollage.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>
<!-- broadcast receiver that listens actions/event associated with NEW PICTURE -->
<receiver
android:name=".TestReciever"
android:enabled="true"
android:priority="999" >
<intent-filter>
<action android:name="com.android.camera.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_PICTURE" />
<action android:name="android.provider.MediaStore.ACTION_IMAGE_CAPTURE" />
<action android:name="com.google.android.glass.media.CameraManager.ACTION_IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</receiver>
<!-- broadcast receiver that listens actions/event associated with a TAP-->
<!--
<receiver android:name=".TestReciever" android:exported="false">
<intent-filter android:priority="999" >
<action android:name="com.google.glass.action.TAP" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
-->
</application>
</manifest>
In my first attempt a I tried to capture image capture event & I tried all combinations of filter such as:
<action android:name="com.android.camera.NEW_PICTURE" />
<action android:name="android.hardware.action.NEW_PICTURE" />
<action android:name="android.provider.MediaStore.ACTION_IMAGE_CAPTURE" />
<action android:name="com.google.android.glass.media.CameraManager.ACTION_IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
But nothing is being logged or printed to the console.
In my 2nd attempt I tried registering for another event in this case TAP event , but I cant get onReceive() to be called for that either, the code is simple I'm either missing something very small or their is a bug in XE19.1 release for Glass.
Any intellegent help will be appreciated.
Thanks
I don´t know if this is part of your problem... I´m still learning to use the Glasses, but in your manifest you are missing
uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT"
Also, are you registering the reciever in the Main class? For example, I'm using in an app the bluetooth and the Broadcastreviever I register it at onCreate() like
Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); // Register the BroadcastReceiver
registerReceiver(mReceiver, Filter); // Don't forget to unregister during onDestroy
Where mReceiver is in my case is
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { // Create a BroadcastReceiver for ACTION_FOUND
public void onReceive(Context context, Intent intent) {
// Methods...
}
}
Hope this helps!

Broadcast Receiver not receiving ACTION_BOOT_COMPLETED

I'm trying to do a basic BroadcastReceiver that can receive the Action_BOOT_COMPLETED. Whenever I run this in the emulator, it doesn't seem like the code from the BroadcastReceiver code.
Manifest as follows:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BootAtStartupActivity"
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=".BootAtStartupReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity:
package com.mfcoding.android.bootatstartup;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class BootAtStartupActivity extends Activity {
static final String TAG = "BootAtStartupActivity";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "onCreate");
}
}
BroadcastReceiver:
package com.mfcoding.android.bootatstartup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootAtStartupReceiver extends BroadcastReceiver {
static final String TAG = "BootAtStartupReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d(TAG, "*** onReceive ACTION_BOOT_COMPLETED");
}
Log.d(TAG, "*** onReceive");
}
}
In Logcat, I never see the Log printout for the BroadcastReceiver file. All i see in Logcat is the Activity log printout. Any ideas?
I'd like to see in Logcat the Log print statements of the Broadcast Receiver class.
Link to project
https://github.com/fangstar/BootAtStartup
An app can only receive this Broadcast Intent after the first device reboot occurring after the app has been installed and executed at least once.
Also note that if the app is installed on external storage it may never receive this broadcast because the external storage gets mounted after the Broadcast has been sent.
try removing this line:
<category android:name="android.intent.category.DEFAULT" />
from your intent filter in the manifest. I've successfully implemented boot listener without this line so I know that it is not needed, however I don't know for sure if it would cause it to not work. Either way it is worth a shot though.
You want to register your broadcast reciever in oncreate() method of your activity.
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
receiver - BroadcastReciever

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.

Categories

Resources