I am trying to make my app print something to log when screen is turned on but it doesn't work as I expected.
Here is what I have in my Manifest file
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity ...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="PhoneBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
</application>
and my receiver looks like
public class PhoneBroadcastReceiver extends BroadcastReceiver {
public PhoneBroadcastReceiver()
{
}
#Override
public void onReceive(Context _context, Intent _intent) {
// TODO Auto-generated method stub
String a = _intent.getAction();
MessageHandler.log("Received action: " + a); // just a wrapper for printing to a log
}
}
but it prints nothing to the log. I keep pressing my Android power button and the screen turns on / turns off but my message doesn't appear in the log. What am I missing? It looks the same as in examples I found on the web.
You cannot listen for ACTION_SCREEN_ON broadcasts via a BroadcastReceiver registered in the manifest. You have to register it via registerReceiver() from a running component. There are fairly few broadcasts with this trait (ACTION_SCREEN_OFF, ACTION_BATTERY_CHANGED, and perhaps ACTION_USER_PRESENT).
ACTION_SCREEN_ON won't work if registered via manifest file. You need to register it dynamically.
maybe you forget to register the service on
<application> tag:
<service android:name=".YourService" />
Related
I am working on an app In this, I have to send the current status of the android devices in 30-30 seconds through my app which I have done. But the problem I am facing is the app is not starting after boot complete this problem is related to some of android devices.
MY Code is:
public class BootCompletedReceiver extends BroadcastReceiver {
private static final String TAG = "BootCompletedReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"action : "+intent.getAction());
if (Objects.requireNonNull(intent.getAction()).equals(Intent.ACTION_BOOT_COMPLETED)){
Log.d(TAG,"receive boot completes : "+intent.getAction());
#SuppressLint("StaticFieldLeak")
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object[] objects) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
context.startForegroundService(new Intent(context, StatUpdateService.class));
}
};
task.execute();
}
}
}
Manifests file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<......./>
<receiver
android:name=".receiver.BootCompletedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
List item
On Some of the android devices, This Code is working and My app is starting after boot complete, but on the Some of the android devices, it's not working.
Android does not auto-start any application until you manually launch it at least once. After that, the applications will automatically start on each Android boot.
Add android.permission.RECEIVE_BOOT_COMPLETED permission in your manifest.
When Android finishes booting and is ready to start the home activity, the home event is sent and qualifying applications identify themselves as bootable candidates. The system sends out the android.intent.category.HOME and android.intent.category.DEFAULT intents when it's done initializing.
<?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.test.app">
<!-- add this -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:theme="#style/app_theme_red">
<!-- main -->
<activity
android:name=".ui.activity.MainActivity"
android:exported="true">
<!-- main filter -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- add this -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- boot receiver -->
<receiver android:name=".BootReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
I have been trying to receive broadcast message send by system e.g ACTION_CAMERA_BUTTON. I have been trying to receive the same in our application.
Following is my code
Android Manifest.xml
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Uri uri = intent.getData();
Toast.makeText(context,"Got Intent",Toast.LENGTH_LONG).show();
Log.e("Tag",uri.getHost());
}
}
I ran the application in debug mode. When i click the camera button of my device, there is no OnReceive event fired in my app.
What i am missing ?
Do i need to register the broadcast in my main.activity also ?
Add these to your manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
For Camera you need to take runtime permission from user because your Api level 23.
This question already has answers here:
Android BroadcastReceiver on startup - keep running when Activity is in Background
(6 answers)
Closed 6 years ago.
In order to catch android.intent.action.BOOT_COMPLETED events, is it enough just to register the BroadcastReceiver in the AndroidManifest.xml? like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myDomain.myApp"
android:installLocation="internalOnly">
<!-- ... stuff ... -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- ... stuff ... -->
<application ...>
<!-- ... stuff ... -->
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
</receiver>
<!-- ... stuff ... -->
</application>
</manifest>
The BroadcastReceiver:
package com.myDomain.myApp.bgServices;
// imports ...
public class MyBootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("my-log", "MyBootBroadcastReceiver.onReceive()");
Toast.makeText(context, "YEAY!", Toast.LENGTH_LONG).show();
}
}
I'm asking because I'm sending:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.myDomain.myApp/.bgServices.MyBootBroadcastReceiver
But nothing happens.
(and I run the app, not just pushing it to the device)
Add the following IntentFilters to your BroadcastReceiver:
<receiver android:name=".bgServices.MyBootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Although android.intent.action.BOOT_COMPLETED should be enough, it seems some devices require android.intent.action.QUICKBOOT_POWERON also.
For more information, you should take a look at the developer's guide on Intents and IntentFilters.
No That is not. You did not specified the component which will receive the ACTION android.intent.action.BOOT_COMPLETED. So you need to add intent-filter to your receiver
You should do it like below.
<receiver android:name=".bgServices.MyBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
I'm beginning to learn Android and have been reading the "Beginning Android 4 Application Development." (As well as downloading the relevant source code)..
However; I have been trying to create a very simple slideshow with a button labelled "Gallery" which will take me to a new Activity that will show a grid like layout for my photos. However, my application does not do this. When the button is pressed it either crashes the app or refuses to do anything at all.
Manifest
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:label="Viwer"
android:name=".Viwer" >
<intent-filter >
<action android:name="com.example.viwer.Gallery" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Any help would be greatly appreciated, I assume it's something simple but after looking at it for three hours I can't see what it is.
You need to add the Gallery activity to the Android Manifest
<activity
android:name=".Gallery"
android:label="Gallery"></activity>
In your manifest you register viewer activity in an intent-filter yet is not declare as a broadcast receiver.
Second event if it was a broadcast receiver it will never launch as you do not send any broadcast by calling startActivity
i think that what your trying to achieve is this:
Please try this:
ebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Viwer.calss )
startActivity(intent);
}
});
I configured Parse API in my app and everything works except push notifications. I tried to send them from the site but they don't arrive in app.
I did everything as written in documentation, but I'm not able to receive notification pushes.
I was able to receive one and when I pressed it app crashed. Now I tried to change something but even with reversing I cannot receive them anymore. How can I resolve my problem?
EDIT: Since some users may be interested, here's the parts of the code I used for push notifications:
Main Class (not MainActivity though):
public class InstantsApplication extends Application {
public void onCreate() {
super.onCreate();
// Hidden
Parse.initialize(this, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
}
}
AndroidManifest (permissions not here included, trust me I have put them):
<!-- PARSE -->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
-->
<!-- I have hidden package name root for this question -->
<category android:name="com.xxxxxxxx.instants" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
I met this problem before, that because you use the newest Parse api.
There are just a few changes you need to make.
First, to fix the error directly caused by issuing the push from the Parse backend you need to declare a notification icon for the Parse push in your Manifest.
<meta-data android:name="com.parse.push.notification_icon"
android:resource="#drawable/ic_launcher"/>
use before the closing application-Tag.
Issuing another push from the backend now will give you a push notification as you expect. So far so good. Clicking the push will result in an app crash again. To fix this you need to remove the now deprecated call PushService.setDefaultPushCallback(...) and add your own Receiver class. I did this in the *.util package just as the following:
public class Receiver extends ParsePushBroadcastReceiver {
#Override
public void onPushOpen(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Next, change the default Parse receiver to the one just created: - Go to your Manifest file.
<receiver
android:name="your.package.name.utils.Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>