I'm developing an app and i need another external application with a broadcast receiver only. Here is my code:
app1:
Intent intent = new Intent();
intent.setAction("com.blabla.myaction");
intent.putExtra("extra", "test");
sendBroadcast(intent);
app2 (The one with the receiver):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver
android:name=".myReceiver">
<intent-filter>
<action android:name="com.blabla.myaction" />
</intent-filter>
</receiver>
</manifest>
public class myReceiver extends BroadcastReceiver {
private Context mContext;
public static final String ACTION = "com.blabla.myaction";
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
if (ACTION.equals(intent.getAction())) {
Log.e("lala", "received");
String extra = intent.getStringExtra("extra");
if (packageName != null) {
Log.e("lala", extra);
}
}
}
With this, i doesn't get the "received" log nor the extra. Why?
You "recieving application has to have been started at least once.
You may want to review https://developer.android.com/reference/android/content/BroadcastReceiver.html.
Related
This is little snipet to try BroadcastReciever android but i dont understand wath's wrong.
In fact just callIntent return log. my little brain it's burning.
if somebody can tell me why ?
Sorry for my bad english !
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.plectre.broadcast">
<application
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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReciever" >
<intent-filter>
<action android:name="com.plectre"/>
</intent-filter>
</receiver>
</application>
</manifest>
```
public void callIntent() {
Intent intent = new Intent();
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
```
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
```
No error, no crash, nothing !! :=(
Create an explicit intent by explicitly declaring the receiver when you create the intent. At present your intent is implicit.
public void callIntent() {
// Explicitly declare the receiver class
Intent intent = new Intent(this, MyReciever.class);
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
Additional reading: https://developer.android.com/guide/components/intents-filters#ExampleExplicit
Also. You declare the intent's Extra as "frist" and try to receive at as "first". This needs to be rectified.
You should ensure that the correct action is taken with the specified Intent Action in your receiver:
#Override
public void onReceive(Context context, Intent intent) {
if ("com.plectre".equals(intent.getAction())) {
final String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
}
I'm studying Android.
I try to implement a Custom static Broadcast Receiver but it is not working.
I search for some issue from Google but I can't find something to solve this.
I work on Android 7.0 Min Level 24 Target Level 28
In fact, MyStaticReceiver isn't launching when I start the Activity (no log)
MyDynamicReceiver work perfectly
Do you have a solution?
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.receiver">
<application
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="test.receiver.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyStaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="#string/StaticAction" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java :
public class MainActivity extends Activity {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
private MyDynamicReceiver dynamicReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName receiver = new ComponentName(this, MyStaticReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
#Override
protected void onResume() {
super.onResume();
if (Debug) Log.i(TAG, "MainActivity:onResume");
configureDynamicReceiver();
}
#Override
protected void onDestroy(){
super.onDestroy();
if (Debug) Log.i(TAG, "MainActivity:onDestroy");
unregisterReceiver(dynamicReceiver);
}
public void onClickButton(View v) {
if (Debug) Log.i(TAG, "MainActivity:onClickButton");
Intent staticIntent = new Intent();
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
Intent dynamicIntent = new Intent();
dynamicIntent.setAction(getString(R.string.DynamicAction));
sendBroadcast(dynamicIntent);
}
public void configureDynamicReceiver() {
if( dynamicReceiver == null ) {
dynamicReceiver = new MyDynamicReceiver();
}
IntentFilter filter = new IntentFilter(getString(R.string.DynamicAction));
registerReceiver(dynamicReceiver, filter);
}
}
MyStaticReceiver.java (MyDynamicReceiver is the same ...)
public class MyStaticReceiver extends BroadcastReceiver {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
public MyStaticReceiver() {
if (Debug) Log.i(TAG, "MyStaticReceiver");
}
#Override
public void onReceive(Context context, Intent intent) {
if (Debug) Log.i(TAG, "MyStaticReceiver:onReceive");
}
}
You are actually sending an implicit broadcast, therefore the receiver declared in the manifest will not work.
If your app targets Android 8.0 (API level 26) or higher, you cannot use the manifest
to declare a receiver for most implicit broadcasts (broadcasts that
don't target your app specifically). You can still use a
context-registered receiver when the user is actively using your app. Link
You don't face any issue with MyDynamicReceiver because it is context-registered receiver.
But to make it work for MyStaticReceiver, you can try sending an explicit broadcast by passing the component name in the constructor of the Intent.
Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
The function "onReceive" is called when BroadcastReceiver is Registered in the Manifest but NOT called if registered dynamically.
The code that works is below:
public class EyeGesture extends BroadcastReceiver {
//Eye Gesture
private static IntentFilter eyeGestureIntent;
private static Context eyeGestureContext;
private static StringBuilder gestureInfo = null;
private static BroadcastReceiver broadcastReceiver;
// public void startEyeListening() {
//Eye Gesture
//}
#Override
public void onReceive(Context context, Intent intent) {
// this = context;
if (intent.getStringExtra("gesture").equals("WINK")) {
Log.e("WINKED ","");
}else {
Log.e("SOMETHING", "is detected " + intent.getStringExtra("gesture"));
}
//Disable Camera Snapshot
// abortBroadcast();
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntent = null;
eyeGestureContext = null;
gestureInfo = null;
}
}
Below is the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.inno.glassplugin" >
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainFunct"
android:icon="#drawable/ic_glass_logo"
android:label="#string/title_activity_main_funct" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger" />
</activity>
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
</application>
</manifest>
The problem is that "onReceive" is NOT called when registered dynamically. I have to do this in a dynamic way.
Below is the code that is NOT working code.
public class EyeGesture extends Activity {
//Eye Gesture
IntentFilter eyeGestureIntentFilter;
Context eyeGestureContext;
BroadcastReceiver broadcastReceiver;
public EyeGesture(){
Log.e("CONSTRUCTOR ", "");
eyeGestureContext = MainFunct.getCurrentContext();
eyeGestureIntentFilter = new IntentFilter("com.google.glass.action.EYE_GESTURE");
eyeGestureIntentFilter.setPriority(1000);
startRunning();
}
void startRunning(){
eyeGestureContext.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Received ", " Something");
}
},eyeGestureIntentFilter);
}
#Override
public void onResume(){
super.onResume();
}
#Override
public void onPause(){
super.onPause();
unregisterReceiver(broadcastReceiver);
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntentFilter = null;
eyeGestureContext = null;
}
}
Also, I don't want to extend BroadcastReceiver from this class. Why am I not receiving anything if registered dynamically. I also removed the following line from the Manifest:
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
but still, it is not working. There is no error or exception thrown.
What am I doing wrong?
Are you using explicit intent? It seems that dynamically registered broadcast receivers cannot receive explicit intents. Implicit intents work.
For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
If the issue is not explicit intents but if you are using LocalBroadcastManager for sendBroadcast then make sure that the registerReceiver is also called of LocalBroadcastManager and not of Context
Try using ApplicationContext instead of Activity.
Modyifing line:
eyeGestureContext = MainFunct.getCurrentContext();
I would try things in this order:
eyeGestureContext = getApplicationContext();
eyeGestureContext = getApplication();
If above does not work I would extend the Application and do:
public class MyExtendedApplication extends Application {
private static MyExtendedApplication instance;
public static MyExtendedApplication getInstance() {
return instance;
}
}
This works for me with global "android.net.conn.CONNECTIVITY_CHANGE" broadcast
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
so should also for you with "com.google.android.glass.action.EYE_GESTURE"
Watching adb logcat in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never hits the event bus; instead, it skips straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button. So it looks like the eye-gesture API was removed without announcement.
The receiver should extend the BroadcastReceiver class.
Define the receiver in the manifest
In the code (maybe onCreate), register the receiver
Create a receiver object
Define the intent filters
call RegisterReceiver() passing in the receiver and the intent filters
I am a newbie to Android development. I am trying to invent a broadcast receiver from my activity.
public class MyReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"ABC", Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
String[] parameters= (String[])intent.getSerializableExtra("parameters");
}
}
my activity is
public class MyActivity extends Activity {
public static String BROADCAST_ACTION="com.kiosk.cbal.CALL_RECEIVER";
/**
* #see android.app.Activity#onCreate(Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String parameters = "safdsam,fdsa,fdsa,fdsa,fdsa";
String[] parameters =abc.split(",");
Intent i = new Intent("com.package.MyReceiver");
i.putExtra("parameters", parameters);
sendBroadcast(i);
}
}
My Manifest file is
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.package" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="7"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name="MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.kiosk.cbal.CALL_RECEIVER"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
</manifest>
Now how I'll Be able to give a call to broadcast receiver through an activity?
Remove the space from the action name. Use the name when you create the Intent.
Update: The code above still has Intent i = new Intent("com.package.MyReceiver");. It should be Intent i = new Intent("com.kiosk.cbal.CALL_RECEIVER");
Meanwhile, in your manifest the receiver name is specified as <receiver android:name="MyReceiver">. note that android:name must be either a fully qualified class name, or a name relative to the package name. The way PackageManager distinguishes the two is by the presence of a . at the beginning of the name. Thus, with your declaration, PackageManager is most likely attempting to instantiate MyReceiver' instead ofcom.package.MyReceiver`.
In any case, you should check the Android log file for details on what's going wrong with the intent you're broadcasting.
The string that you pass to the Intent constructor must be the same as the string specified for the action element for your receiver in the manifest. You are constructing the Intent with your receiver class name, but your receiver is declared to listen to the VIEW intent instead.
I've have a simple app to schedule a notification 5 minutes after a button is pressed. It works fine for me. But if I restart the phone within that 5 minutes I don't get the notification. I have done a research on Alarm Manager and Scheduling Notifications on device reboots. I have a basic idea but I really don't know how to implement it into my project. I have 4 classes in my project. They are:
MainActivity
NotificationUtil
NotificationPublisher
NotificationView
This is the my NotificationUtil class:
public class NotificationUtil
{
public static void createNotification(Context context,Class<?> cls, String title, String content)
{
Intent intent = new Intent(context,cls);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
sendNotification(getNotification(pendingIntent,context,title,content),context);
}
private static void sendNotification(Notification notification,Context context)
{
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION,notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5*60 * 1000,pendingIntent);
}
private static Notification getNotification(PendingIntent pendingIntent, Context context, String title, String content)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"ChannelID");
builder.setSmallIcon(R.drawable.notification_bell);
builder.setContentTitle(title);
builder.setContentText("You have a Notification");
builder.setSubText("Tap To View");
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(content));
builder.setContentIntent(pendingIntent);
return builder.build();
}
}
This is my NotificationPublisher class:
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
#Override
public void onReceive(final Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int notificationId = intent.getIntExtra(NOTIFICATION_ID, 1);
notificationManager.notify(notificationId, notification);
}
}
This is how I call the NotificationUtil class on button click in the MainActivity:
public class MainActivity extends AppCompatActivity {
private Button button;
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.notification);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NotificationUtil.createNotification(MainActivity.this,NotificationView.class,"Notification","You have a new Task");
}
});
}
}
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.notificationtest">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationView"
android:parentActivityName=".MainActivity"/>
<receiver android:name=".NotificationPublisher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
I have created a new DeviceBootReceiver class:
public class DeviceBootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
}
}
}
But I'm not sure what to put in the onReceive function. I tried putting this in the onReceive function
Intent pushIntent = new Intent(context, NotificationPublisher.class);
context.startService(pushIntent);
Works normal, but if I reboot my phone, after 5 minutes I get a message "The Application has stopped working"
I have a basic idea after having gone through these tutorials but I don't know how to implement them into my project
https://www.stacktips.com/tutorials/android/repeat-alarm-example-in-android
https://droidmentor.com/schedule-notifications-using-alarmmanager/
What I need is, to get the notification even after my phone is rebooted. If any one out there who could help me to achieve this, I would be grateful.
you need to create a BroadcastReceiver in order to be able to listen REBOOT event that is delivered by OS and then you can start your alarm manager there again
Well here is a complete example of an AutoStart Application.
Give permission of RECEIVE_BOOT_COMPLETED in manifest file, and register your broadcast for BOOT_COMPLETED.
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".hello"></activity>
<service android:enabled="true" android:name=".service" />
</application>
</manifest>
autostart.java
public class autostart extends BroadcastReceiver
{
public void onReceive(Context context, Intent arg1)
{
// This callback will be fired automatically when device starts after boot
// Do your alarm alarm manager work here
}
}