Hello i create one broadcast test but no work
Manifest:
<receiver android:name=".BeaconsBroadcast"
android:exported="false">
<intent-filter>
<action android:name="com.example.android.kontacktestbeacons.BeaconsBroadcast"/>
</intent-filter>
</receiver>
in my MainActivity:
protected void onStop() {
super.onStop();
try{
Log.e("ENTRO ","ENTRO");
Intent i = new Intent();
i.setAction("com.example.android.kontacktestbeacons.BeaconsBroadcast");
startService(i);
}catch (Exception e){Log.e("ERROR","ERRR");}
}
mi broadcast class:
public class BeaconsBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("ENTRO ","EBTROPOOOOO RECIVE");
Toast.makeText(context, "Se ha pulsado el botón.", Toast.LENGTH_SHORT)
.show();
}
}
in logcat:
12-02 11:12:59.551 28588-28588/com.mydomain.myapplication W/ContextImpl﹕ Implicit intents with startService are not safe: Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } android.content.ContextWrapper.startService:494 com.example.android.kontacktestbeacons.MainActivity.onStop:101 android.app.Instrumentation.callActivityOnStop:1235
12-02 11:12:59.552 927-2008/? W/ActivityManager﹕ Unable to start service Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } U=0: not found
where my error?
You are declaring BeaconsBroadcast as a BroadcastReceiver but are using startService(i) to invoke it which is causing the issue.
You need to use the sendBroadcast(i) function to send a broadcast to a BroadcastReceiver.
Also Since you have set android:exported="false" for BroadcastReceiver use
LocalBroadcastManager.getInstance(Context context).sendBroadcast(Intent)
Hello im fine by manifest
<receiver android:name="BeaconsBroadCast">
<intent-filter>
<action android:name="com.tutorialspoint.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
in application tag
Related
I would like my app to detect if a new outgoing call happened
I want to do something like this :
if (there was a new outgoing call) {
do...
} else {
do...
}
You can use the android Broadcast Reciever. To use it all you have to do is first,
Create a new handler class to handle the broadcast receiver.
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//You can do this to get the phone number, it is only optional
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//Handle outgoing call event here
}
}
Then register it in the android manifest so it knows where this method is.
<receiver android:name=".OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
Finally, create a permission in the android manifest.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
And that should do it, hope I could help. :)
Create a broadcast receiver to catch the outgoing calls:
public class CallReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// your code
}
}
Then register it in the manifest:
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Lastly set the permissions:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Detect outgoing phone call event
1)Create OutgoingCallBroadcastReceiver
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
}
Register OutgoingCallBroadcastReceiver in AndroidManifest.xml
Add permission in AndroidManifest.xml
This is what I have so far but nothing happens when I input this combination in dialer
public class DialReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, final Intent intent) {
if (intent.getAction().equals(android.content.Intent.ACTION_NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getExtras().getString( android.content.Intent.EXTRA_PHONE_NUMBER );
if(phoneNumber.equals("*#588637#")) {
Intent intent1 = new Intent(context , Activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent1);
}
}
}
}
and in androidmanifest
<receiver
android:name=".receiver.DialReceiver"
android:exported="true"
android:process=":background"
tools:ignore="ExportedReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Try with these small changes..
String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");
if(phoneNumber.equals("*#588637#")) {
//do your stuff
}
And do not forget to add this line in your Manifest.xml file
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Also you may find these helpful..
http://android.programmerguru.com/simple-dialer-application/
http://tikuflower.blogspot.com/2011/12/android.html
Is the receiver getting the broadcast at all? If not, maybe you forgot to include the PROCESS_OUTGOING_CALLS permission.
Try This,
Add to this in manifest,
here host is 12456, so your secret code is *#*#123456#*#* (dial-in dialped)
<receiver android:name=".Dialer"> //here is your broadcast receiver class
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code"
android:host="123456"
/>
</intent-filter>
</receiver>
here is your Broadcast Receiver class :
class Dialer : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// Declare Here your launcher activity in Intent
var i : Intent = Intent(context, MainActivity::class.java)
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context!!.startActivity(i);
}
}
According to ridoy's 2nd link,
http://tikuflower.blogspot.com/2011/12/android.html
It should be
String phoneNumber = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
rather than
String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");
That change works for me at least...
I'm trying to show a message when a user places a call using the standard android dialer.
I have the following code in my Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTestButtonListener();
Log.i(LOG_TAG, "Activity started...");
}
private void setTestButtonListener()
{
Button testButton = (Button)this.findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.i(LOG_TAG, "Clicked on test...");
Toast.makeText(getApplicationContext(), (CharSequence)"You clicked on the test button" ,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+150));
MainActivity.this.sendOrderedBroadcast(intent, null);
MainActivity.this.startActivity(intent);
Log.i(LOG_TAG, "intent broadcasted... I think... ");
}
});
}
Then in the BroadcastReceiver (well a class that derives from it):
public class OnMakingCallReceiver extends BroadcastReceiver
{
private static final String LOG_TAG = OnMakingCallReceiver.class.getSimpleName() + "_LOG";
#Override
public void onReceive(Context context,Intent intent)
{
Log.i(LOG_TAG, " got here!");
}
}
And then in the AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE" />
<receiver android:name=".OnMakingCallReceiver" android:priority="999">
<intent-filter>
<action android:name="android.intent.action.CALL"/>
</intent-filter>
</receiver>
I click the test button and I see this output.
Clicked on test...
intent broadcasted... I think...
And thats all. I expected to see "got here!".
Any ideas why I don't?
Did you define the receiver in your AndroidManifest.xml?
Also, ACTION_CALL_BUTTON is sent when you click on something that goes directly to the dialer. Are you sure you're doing that.
I used this in AndroidManifest.xml and it works. I think the key thing is the intent NEW_OUTGOING_CALL in the intent-filter.
<receiver android:name=".OnMakingCallReceiver" android:exported="true" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.ACTION_CALL" />
</intent-filter>
</receiver>
Probably ACTION_CALL could be removed. Also this may need to be added to the manifest:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
Although the Java constants are android.content.intent.ACTION_USER_PRESENT, android.content.intent.ACTION_BOOT_COMPLETED, and android.content.intent.ACTION_SCREEN_ON, the values of those constants are android.intent.action.USER_PRESENT, android.intent.action.BOOT_COMPLETED, and android.intent.action.SCREEN_ON. It is those values which need to appear in your manifest.
Note, however, that a receiver for ACTION_SCREEN_ON can not be declared in a manifest but must be registered by Java code, see for example this question.
Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
Add manifest tag
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
Create a receiver class and add your codes
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Create a service and register the receiver in it
public class MyService extends Service {
MyReceiver receiver = new MyReceiver();
#Override
public IBinder onBind(Intent intent) { return null; }
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(receiver);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Don't forget to define the service in manifest
<service android:name=".MyService"/>
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like alarm managers, jobs, etc which is not related to this question.
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in receiver tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:
<receiver android:name=".MyReciever">
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</receiver>
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck
I have a receiver that works well, but I can't seem to show a proper UI, although the toast appears correctly. As far as I can tell, this is caused by Android requiring the class to extend Activity, however, the class already extends BroadcastReceiver, so I can't do this.
So, I tried to do an Intent, but this failed too. There are no errors, but the screen doesn't show. Source code is below.
Broadcast (Method in AndyRoidAlarm)
public void setAlarm(){
Intent intent = new Intent(AndyRoidAlarm.this, Reciever.class);
PendingIntent sender = PendingIntent.getBroadcast(AndyRoidAlarm.this,
0, intent, 0);
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
// Tell the user about what we did.
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(AndyRoidAlarm.this, "Alarm Scheduled for 30secs", Toast.LENGTH_LONG);
mToast.show();
}
Reciever
public class Reciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClass(context, AlarmRing.class);
}
}
Reciever V2
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
Intent foo = new Intent(context, AlarmRing.class);
//foo.putExtra("id", "id");//example, if you wish to pass custom variables
context.startActivity(foo);
}
AlarmRing
public class AlarmRing extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.sweetchild);
mp.start();
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.comaad.andyroidalarm"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndyRoidAlarm"
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.comaad.andyroidalarm.Reciever" android:enabled="true">
<intent-filter>
<action android:name="com.comaad.andyroidalarm.Reciever"></action>
</intent-filter>
</receiver>
<activity android:name=".AlarmRing"></activity>
</application>
</manifest>
}
In a BroadcastReceiver onReceive() method, if you need a Context (e.g., to create an Intent), use the Context that is passed to you as a parameter of onReceive(). You even have this code in your onReceive() -- you're just not doing anything with the resulting Intent (e.g., calling startActivity()).
Intent foo = new Intent(this, AlarmRing.class);
foo.putExtra("id", id);//example, if you wish to pass custom variables
this.startActivity(foo);
Edit
Check out this example to use BroadcastReciever within an Activity. http://almondmendoza.com/2009/01/04/getting-battery-information-on-android/