Debug app that runs at boot - java

I am using Android Studio.
I added a BroadcastReceiver for my app that receives android.intent.action.BOOT_COMPLETED, the receiver just shows up a Toast for testing. The problem is I get "App has stopped" message just after Android starts up.
My first question is: is there anyway to debug that at startup and see where is the problem by myself? Because I can't see any log referring to that problem in Android Studio.
My second question is related to the problem itself. Here is the code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp">
<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">
<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=".AutoStartReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Java: BroadcastReceiver
public class AutoStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "loaded", Toast.LENGTH_LONG).show();
throw new UnsupportedOperationException("Not yet implemented");
}
}
The third question is: Is it OK to do some heavy work (read some files and set the AlarmManager) in the receiver rather than creating a service? because as of API 26 Android is putting a lot of restriction to services.
Thank you

For the debugging part I used this (in the Terminal, when the emulator is on) :
adb shell am set-debug-app -w --persistent <your.app.package>
to start debugging, then click Attach debugger to android process when the app prompts for it on the emulator.
To disable this:
adb shell am clear-debug-app <your.app.package>
Found answer here : https://medium.com/#elye.project/debug-app-deeplink-at-launch-time-bdb2cf04a9e6

Apps that launch on boot can be debugged the second your device gets a debugging connection, which usually happens just before the app itself would boot. Just open logcat and watch for the device and app to pop up after reboot. Note that this assumes the app is debuggable. Apps that aren't will simply not show any logs.
You get the MyApp has unfortunately stopped message for the obvious reason of this code:
public class AutoStartReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "loaded", Toast.LENGTH_LONG).show();
throw new UnsupportedOperationException("Not yet implemented");
}
}
You throw an exception, meaning it'll stop.
As for what you do in the service, as long as it uses within a reasonable amount of RAM and processor (extremely heavy services are more likely to be killed to save battery and memory) you're good to go

Related

Broadcast Reciever, ¿Am I doing it right?

first question here...
I have this practice from my college, but the course sometimes doesn't explain everything, and it's about broadcast and receiving a Toast message.
(Some things are going to be in Spanish)
You see, the first app it's about having a view with a button:
Activity View
The only thing that it does is sending a message through a button, and the OnClick has linked this method that is on the Activity, which name is Emisora.java (there is no main activity, but it is configured to this be the launch activity):
public void Emision(View v){
Intent intent = new Intent();
intent.setAction("com.tecmilenio.practica91");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
The Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmilenio.practica91">
<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=".Emisora">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Then I have the other app, which doesn't have an activity but it does have an Broadcast Reciever:
package com.tecmilenio.receptor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadcastReceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Transmission Recieved", Toast.LENGTH_SHORT).show();
}
}
(I know having a Toast for this is not the best practice but is just for the practice of the course)
And its Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmilenio.receptor">
<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">
<receiver
android:name=".BroadcastReceptor"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.tecmilenio.practica91"/>
</intent-filter>
</receiver>
</application>
</manifest>
And when I install and execute the first one it just doesn't happen anything, I tap the button but nothing happens.
Then I later saw that in the second project (the receiver) when I execute it from the Android Studio, the app doesn't appear in the phone, neither execute anything, but i thought this was normal due to the lack of an activity in it. Then I also saw that when executing through the studio also the Run console sends me the message "Timed out waiting for process (com.tecmilenio.receptor) to appear on xiaomi-mi_8-2ef63c6e.", maybe this has something to be (?)
I would appreciate that someone explains me this... Thanks!
Update
I have found that the problem has to be a lot with the Timeout on launch, adding an activity with nothing and launching that activity makes it work, however that is not what it's supposed to do, it is supposed that it shouldn't have any activity, and should execute the OnRecieve when the "practica91" app sends the message...
Is there any way of how to do this?
Set action declared in manifest and sending broadcast from an activity is different.
try using the same action at both the places -
Intent intent = new Intent();
intent.setAction("com.tecmilenio.practica91");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
I think there is a small mistake here. You're sending as an action in the first app
the following string com.tecmilenio.emision. Whereas in the second app you're registering the broadcast's action as below
<action android:name="com.tecmilenio.practica91"/>
The first change would be to rename the broadcast action to com.tecmilenio.emision.
<receiver
android:name=".BroadcastReceptor"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.tecmilenio.emision"/>
</intent-filter>
</receiver>
Another important point is that when sending the broadcast add FLAG_INCLUDE_STOPPED_PACKAGES flag to the intent because when you broadcast from app A to app B , app B might not be running, this flag insures that the broadcast reaches out even apps not running:
Regards I'm Mexican too!

I am trying to build an app, that creates a notification when wifi state is changed

I am building an app that sends a notification when wifi connection is established. Since I am using android 9 setting intent filter in AndroidManifest.xml doesn't work.
I created a custom BroadcastReceiver, that creates a notification, and it works if I register receiver in onCreate.
Here is the registerReceiver part of the code in onCreate (it works when app is opened)
receiver = new WifiStateReceiver();
IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
Here is my BroadcastReceiver:
public class WifiStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TEST", "IT WORKS!");
}
}
And here is my AndroidManifest.xml
.
.
.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
.
.
.
<receiver
android:name=".WifiStateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
.
.
.
After I close my app this doesn't work anymore. So I need some kind of service that never closes, but I'm not sure how battery inefficient this method is. Is there a way to run a service when wifi connection/state changes? And as I already said, putting intent-filter in AndroidManifest doesn't seem to work as I am using android 9.

Broadcast Receiver not receiving BOOT and MEDIA_MOUNTED intent

I'm trying to create a simple BroadcastReceiver that can receive the android.intent.action.BOOT_COMPLETED intent as well as the android.intent.action.MEDIA_MOUNTED intent. The idea is to start a service on receiving either of these intents. Thus the service should be started after Android boot is complete or when an USB storage device is connected to the Android target(if the service is not already started by then).
Permissions used by the application are defined in this section
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
The following section defines the BroadcastReceiver responsible for handling the intents and starting the corresponding service.
<receiver
android:name="com.example.systemupgradeapplication.IntentReceiver"
android:label="USB Detection Receiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
The corresponding service that should be started from the Broadcast Receiver is defined in the following section
<service android:name=".SysUpgradeService" />
Note: The receiver and service components are defined within the <application> section of the Android Manifest.
The following snippet is the Class responsible for handling the intents broadcasted
public class IntentReceiver extends BroadcastReceiver {
private final static String TAG = "IntentReceiver";
private static boolean m_UsbInserted = false;
private static boolean m_UsbRemoved = true;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= ");
// TODO Auto-generated method stub
//if(intent.)
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
String path = intent.getDataString();
if(path.contains("usb")) {
m_UsbInserted = true;
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= "+path);
Intent myIntent = new Intent(context, SysUpgradeService.class);
myIntent.putExtra("path", path);
context.startService(myIntent);
}
}else if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d(TAG, "BOOT Completed intent received");
context.startService(new Intent(context, SysUpgradeService.class));
}
The problem I am facing is that none of the intents are arriving at my broadcast receiver(None of the Logs in the IntentReceiver class are being printed in logcat) even though I can see in the Android Debug logs that the BOOT_COMPLETE and MEDIA_MOUNTED intents are being broadcast. Also This application is not starting after android system is booting up.
I appreciate your help in this regard, what may be wrong with my approach and some possible solutions.
Okay, so I pushed the apk to /system/priv-app which is where System Applications which are part of custom ROM are placed. Now I do not need any activity in my application since it is part of the custom ROM and is recognized as a system application. It seems that if your application is a 3rd party application it must have an activity to be able to receive broadcasted intents.
However in this case I have control over the custom ROM source code as well as root access on the device. So both the approaches work
Make your application part of the custom ROM source, build and flash on device.
Get root access on device, push your apk to /system/priv-app (4.4 onwards), reboot and voila!
Make Sure you have atleat one activity present in your Application.From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

Create an automatic launch-able Broadcast Receiver in Android

Problem:
I am willing to create an application that simply starts as a background process and whenever a new message comes into the device it should log it into a file or simply display a toast message.
I have read a lot of blogs and tried to follow the steps as mentioned. But, I keep on sending messages on my device and nothing displayed not even in device log. I want to run it on devices from Froyo to Lollipop. So, I am not willing to use new Telephony API which supports API 19 and later versions.
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".SMSHandler">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Source File
package com.abc.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SMSHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(context, "message initiated",
Toast.LENGTH_LONG);
toast.show();
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
toast = Toast.makeText(context, "message received",
Toast.LENGTH_LONG);
toast.show();
}
}
}
Environment:
IDE:
Android Studio
Min SDK Version:
8
Tested On:
ICS Device (Sony Xperia U)
Kit-Kat (MOTO G)
You need to add an activity, then run that activity, before this BroadcastReceiver will work.
More accurately, something needs to use an explicit Intent before your app will be moved out of the stopped state and allow manifest-registered BroadcastReceivers to work. The simplest way to do that is to have a launcher activity, and run that activity from the launcher.
To learn more, see "Launch controls on stopped applications" in the Android 3.1 release notes, along with this blog post.
Your Code look like this in manifest file
<receiver android:name=".SMSHandler"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Add the following to your<receiver> in the manifest:
android:enabled="true"
android:exported="true">
Furthermore, according to this thread, it seems that you have to manually start one of your activities before the broadcast receiver will start working, i.e. the application has to have been launched at least once before any broadcast receiver will work.

Custom GCM broadcast reciever not being used on a couple of devices, but works on others

I creating an app that's using GCM to receive notifications from our servers, am using Google's gcm.jar, and have a customGCMIntentService class handling it. My problem is that only one handset is actually getting a response (or more likely routing the response correctly) from GCM when calling GCMRegistrar.register().
What I'm seeing is that the GCMRegistrar (from the gcm library) is correctly setting my custom broadcast receiver as the retry receiver on the nexus 3 phone, the only one that works, but not on the others. This makes me believe that I might be getting a response from GCM but its not being processed on the three other phones (detail below).
GCMRegistrar Setting the name of retry receiver class to <My_application_package>.CustomGCMBroadcastReceiver
All of them have a data connection that allows communication with GCM, all have an active gmail account on them ( all of them have the playstore working ). I've also run Google's GCM demo on them all without a problem.
The registration code:
//This is almost identical to how Google's GCM demo does it.
private void registerOnGCM(){
checkNotNull(SERVER_URL, "SERVER_URL"); //What it says on the tin
checkNotNull(SENDER_ID, "SENDER_ID");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
/*
registerReceiver(mHandleMessageReceiver,
new IntentFilter(HANDLE_MESSAGE));
*/
final String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
if (regId.equals("")) {
// Automatically registers application on startup.
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(getApplicationContext())) {
// Skips registration.
Log.i(TAG, "Already registered");
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context, regId);
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
The code for CustomGCMBroadcastReceiver doesn't matter, nor does the GCMIntentService, since neither of them is ever call'd. Problem isn't with them.
Devices I'm testing on:
HTC Desire GSM running CyanogenMod 7
Google nexus 3 running Android 4.1.1 (Application works on this one)
Motorola T910 running Android 4.0.1
Samsung Galaxy mini running Android 4.2.6
Manifest:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- App has permission to read/write files on sd card. Used for RSS document -->
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="<My_package_name>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="<My_package_name>.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:theme="#style/AppTheme"
android:label="#string/app_name">
<activity
android:name="<My_package_name>.MasterActivity"
android:screenOrientation="portrait">
</activity>
<activity android:name="<My_package_name>.WebActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
BroadcastReceiver that will receive intents from GCM
services and handle them to the custom IntentService.
The com.google.android.c2dm.permission.SEND permission is necessary
so only GCM services can send data messages for the app.
-->
<receiver
android:name="<My_package_name>.CustomGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="<My_package_name>" />
</intent-filter>
</receiver>
<!--
Application-specific subclass of GCMBaseIntentService that will
handle received messages.
By default, it must be named .GCMIntentService, unless the
application uses a custom BroadcastReceiver that redefines its name.
-->
<service android:name="<My_package_name>.GCMIntentService"
android:enabled="true"/>
</application>
Might be something I've forgotten to add. Let me know.
All ideas are welcomed.
-MrDresden
Well it seems to have fixed it self by only cleaning, and rebuilding and then deploying that to those other devices.
Why removing all traces from all phones, then running in debug mode on them didn't work, except on one phone I have no idea.

Categories