Broadcast Receiver doesn't work - android

Main Activity Code. BroadcastExample.java
package com.example.broadcast;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class BroadcastExaple extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("PHONE", "Main Activity...Called");
setContentView(R.layout.main);
Log.d("PHONE", "After Mainf Activity...");
}
}
MyBroadCastReceiver.java
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
/*public void onReceive(Context context, Intent intent) {
try{
Log.d("Call BroadCAST","Calling Broad CAST");
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
catch(Exception e)
{
}
}*/
public void onReceive(Context context, Intent intent) {
Log.d("Message", "Message Received");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast" android:versionCode="1"
android:versionName="1.0">
<uses-prmission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".BroadcastExaple" 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=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
Program doesn't enter into the onReceive Mathod of MyBroadCastReceiver class.

First, i suggest you to format your code here. So that people here will be glad to read your problem code.
Your code list above does NOT register any BroadcastReceiver to the system. You'd better to check out ApiDemo for more details.

Related

Android. Intent.ACTION CAMERA BUTTON is not working

Learning broadcast messages. The book DN Kolisnichenko - "Programming for Android" is an example of where the camera at the touch of a button starts the service. But I have it che does not work. What is the problem? Or I do not know what is the camera button (physical or on the display), or do not understand what is wrong. Here is the code of the example:
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
MyReceiver iReceiver = new MyReceiver();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter iFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
iFilter.addAction(Intent.ACTION_CAMERA_BUTTON);
registerReceiver(iReceiver, iFilter);
}
#Override
protected void onDestroy() {
unregisterReceiver(iReceiver);
super.onDestroy();
}
}
MyReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context rcvContext, Intent rcvIntent) {
String action = rcvIntent.getAction();
if (action.equals(Intent.ACTION_CAMERA_BUTTON)) {
rcvContext.startService(new Intent(rcvContext,
MyService.class));
}
}
}
MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service started...",
Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...",
Toast.LENGTH_LONG).show();
}
}
And manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ivarious.myapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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" />
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
<service android:name=".MyService"></service>
<action android:name="android.intent.action.CAMERA_BUTTON" />
<action android:name="android.intent.extra.KEY_EVENT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
</receiver>
</application>
</manifest>
Make Sure You Added The Camera Permission to your Manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

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.

How to start an app at startup on Android?

I've tried:
This which doesn't work on my phone:
Trying to start a service on boot on Android
This which also fails to work properly:
http://www.compiletimeerror.com/2014/12/android-autostart-app-after-boot-with.html#.VpL6sxWLTIU
And a few others which I couldn't find again. Could someone please post a working example of code which will start a MainActivity with the default HelloWorld xml layout?
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MyService"
android:label="#string/title_activity_my_service" >
</activity>
</application>
</manifest>
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyService extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
}
}
First:create a BroadcastReceiver, onReceive(Context context, Intent intent),start the app you want to startup in this method
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
xxx.class is the app you want to start
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "you want to startup this app.....");
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
} }
Secondly:configure receiver and intent-filter
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
THirdly,add permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Android:Broadcast demo not working

I am new to android, i am trying a broadcast demo, I gave my best effort by reading the documentation but its not working. Please have a look at my code:
BroadcastDemoActivity.java
package com.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BroadcastDemoActivity extends Activity {
/** Called when the activity is first created. */
public static final String PUBLIC_HOLIDAYS = "com.paad.action.PUBLIC_HOLIDAYS";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
}
}
Receive.java
package com.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String message = intent.getStringExtra("Holiday");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BroadcastDemoActivity"
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=".Receive">
<intent-filter>
<action android:name="com.paad.action.PUBLIC_HOLIDAYS"/>
</intent-filter>
</receiver>
</application>
</manifest>
I know that i am missing something which i am not aware of, please help.
I believe your problem is in the call to sendBroadcast.
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
You are not sending the intent that you construct, you're sending the intent returned from getIntent(), which will be the intent that the activity was started with.
It should be
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(intent);

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