notification message only shows when device connected to USB - android

The notification message string in my app only shows if the development device is connected to USB. As soon as I disconnect the phone from USB, even with the app open, the notifications for my app no longer shows the message, it is just blank, yet everything else seems to work fine for the notification.
I have no errors in logcat when running app in ADB, and all other params are passed successfully to the notification. The only issue is the setContentText() only seems to work if phone is connected to computer via USB, even if Android Studio/ADB is not running.
The dev device is an old Samsung SPH-L300 (el-cheapo Virgin mobile phone) running 4.1.2. I don't have another device to test it on. I am using Android Studio 1.5.1 as the IDE.
Am I missing some manifest element? I've heard of a similar problem but it was the other way around, where it only worked when NOT connected to USB. The solution to that, being to change the SDK minimum and target in gradle.build, did not solve my problem (although, I could have missed something when trying that).
The notification is activated using a broadcast receiver from an alarm manager....here is the code:
package com.example.notificationApp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
// get the info needed to identify this specific reminder...
Bundle bundle = intent.getExtras()
int alarmID = bundle.getInt("reminderID");
Intent returnIntent = new Intent(context, HomeActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, alarmID, returnIntent, 0);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// make vibrate pattern...
long[] pattern = {500,500,500,500,500,500,500,500,500};
Notification notify = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Appointment reminder")
.setContentTitle("Reminder...")
.setContentText("TEST TEXT")
.setSound(alarmUri)
.setLights(Color.BLUE, 500, 500)
.setVibrate(pattern)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(alarmID, notify);
}
And here is my Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationApp">
<!-- Permission to start Alarm on device reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".CheckAlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" />
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
I am stumped on this one, anyone have any clues? Thanks in advance!!

So I found the problem and wanted to post it in case anyone else ends up with the same issue. Believe it or not, the problem was in the phone's developer settings with Stay Awake mode on. Turn off Stay Awake in developer settings and voila!
I got the idea from this other post I just found:
https://stackoverflow.com/a/25566924/3716557
(Please direct any upvotes to the answer in link)

Related

Can't start service on boot after Android 7

I want to start a service on boot.
This code only works on Android versions that came before 7.
What should I change to make it work on newer versions? Should I use JobScheduler or maybe WorkManager instead?
For what I know Timer, Handler and AlarmManager are deprecated or have different purposes.
I suspect that after Android 7 they removed the option of starting a service on boot in order to save battery life and avoid slowing down the phone. Can you confirm that?
This is my broadcast receiver
package com.kev.boot21;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("tag","xyz broadcast 1");
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
context.startService(serviceIntent);
}
}
}
This is my service
package com.kev.boot21;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AndroidServiceStartOnBoot extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i("tag","xyz service");
}
}
This is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kev.boot21">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<receiver
android:name="com.kev.boot21.BroadcastReceiverOnBootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.kev.boot21.AndroidServiceStartOnBoot"></service>
<activity
android:name="com.kev.boot21.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>
</application>
</manifest>
I found that WorkManager is the best way to handle this problem.
I managed to run a PeriodicWorkRequest after reboot on Android 7, 8 and 9 using the example code from the official documentation:
https://developer.android.com/topic/libraries/architecture/workmanager/basics
Android 10 behaves in a different way. After reboot the PeriodicWorkRequest works only after opening the app. I guess it's for performance/security reasons
You need to register your receiver in your code by calling Context.registerReceiver(), as ACTION_BOOT_COMPLETED is no longer sent to receivers registered through AndroidManifest.xml

Qt android Can't listen to os intents ex.RECEIVE_BOOT_COMPLETED

I have an android service that runs when i open my app, now I want my android service to run at boot time. I tried the bellow code, but the service is not running automatically when i reboot my device. I cannot see it running as a service on my phone! Is there something wrong in my code?
I added these permissions to the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_HEADSET_PLUG"/>
Here's my receiver in the manifest:
<receiver android:name="org.qtproject.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
<action android:name="android.intent.action.RECEIVE_HEADSET_PLUG"/>
</intent-filter>
</receiver>
And here's MyBroadcastReceiver.java:
import android.os.Bundle;
import org.qtproject.qt5.android.bindings.QtActivity;
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, org.qtproject.example.MyCustomAppService.class);
context.startService(startServiceIntent);
}
}
replace
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
with
<action android:name="android.intent.action.BOOT_COMPLETED"/>
in the manifest
ok so I had 2 problems in my code:
1)Thanks to #kajay, i had to change my action line as he described,
to be:
<action android:name="android.intent.action.BOOT_COMPLETED"/> in the manifest.
2) I was missing defining the package in the MyBroadcastReceiver.java. So, the class couldn't find the startServiceIntent. Of course qt doesn't give any errors or warnings with many java problems.
So, in my case i had to add this to the MyBroadcastReceiver.java :
package org.qtproject.example;
I had to do both of the above steps to fix my problem!
P.S Sometimes the service takes around 45 secs or more to start after booting!

Logcat not working inside onReceive in Broadcast Receiver while listening to deskclock alarm intent

I am listening to deskclock alarm change intent using my broadcast receiver. When the onReceive() method in my Broadcast Receiver is called, the logs (Log.i/v()) inside the onReceive are not getting printed on Android monitor but Toasts are working just fine.
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dumbrella.ratemyday">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<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:process=":remote" android:name="AlarmClockReceiver">
<intent-filter>
<action android:name="com.android.deskclock.ALARM_DISMISS" />
<action android:name="com.android.deskclock.ALARM_SNOOZE" />
<action android:name="com.android.deskclock.ALARM_ALERT" />
<action android:name="com.android.deskclock.ALARM_DONE" />
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast Receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
public class AlarmClockReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// This log.v does not get printed on Android Monitor
Log.v("Broadcast Test", "In Broadcast Listener");
String message = "Broadcast intent detected "
+ intent.getAction();
// This toast gets displayed after the alarm is dismissed
Toast.makeText(context, message,Toast.LENGTH_LONG).show();
}
}
Maybe it's too late, but I leave it for future programmers with this problem, I had a similar problem and solved it this way.
Simply in Logcat panel, change the filtering listbox to "No Filters".
You might want to make sure that you have selected the correct device in your DDMS perspective, also ensure that you have selected the correct filtering option and log level - in your case you want to select "verbose". Also check out the selected answer and suggestions as to why Logcat is not displaying logs.
You can filter logs to show BroadcastReceiver logs in LogCat Filter Configuration section:
The complete solution steps: Logs/logcat not working in BroadcastReceiver

Android App with Broadcast receiver after reboot as main

Hello i would to develop a simple app without Main Activity as launcher.
I want to register a broadcast receiver which starts after reboot of device and inside OnReceive callback starts an Activity
Here my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
And here my Broadcast receiver
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
And finally the MainActivity
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What is wrong in my code?
Thanks in advance
Francesco
my code is working..here is it...
in manifest
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I cant see anything wrong with your code, however i have something worth to try.
Move the permission out of the application tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If its not working, simplify the receiver :
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Still not working? Try to add some delay, as mentioned here :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//run your service
}
}, 10000);
Quoted from the link above :
While, I suggestion to delay several seconds, e.g., 10 seconds, before
running the (1) line, which is more stable for different phones and
services.
For example, in my case, my service is going to write sd card. If you
start your service immediately, some phones may fail because the sd
card is not ready.
Starting with android 3.1 you cannot have a broadcast receiver to get spawned by the application service manager if it has no context in active state (aka. at least an activity or service that is keeping the process in an "active state")
Excerpt from specification
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
You need to somehow start your application, and then send it in a dormant state (but registered in the app manager). You can use a service for this.
It is strongly NOT recommended to start Activity from BroadcastReciever:
https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
Never start an Activity in response to an incoming broadcast Intent.
In my case PackageManager.DONT_KILL_APP helped:
https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
I've experimented with MIUI 8 firmware real device Xiaomi Redmi Note 3.
My findings are:
you have to add app to autorun to enable it be fired by broadcast. I've checked it with such serious apps as Viber, WhatsApp.
I've compared with manifest settings (without enabling reciever programmatically):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

Receive a message on a device with GCM

Actually I am working on the notifications for an Android app.
My two main source of information are this tutorial and the android developer web site.
Let me quickly describe my app:
In my app I use web services (WS) which work with POST HTTP request.
For the Notification I use the GCM system.
My app uses the 'POST HTTP' request system above to be register on my server.
Actually my server does exactly the same job as the one in the tutorial (that means to register an account in the database with to two parameters and the register ID of the device, and send a message to the device with GCM)
Actually all of these steps work:
My app receive successfully gets register ID (gcm.register(SENDER_ID); work)
My app get successfully registered on my server
My server receives a successful message when I try to send a message from my device
{"multicast_id" : 6276079906208554309 , "success" : 1 , "failure" : 0 , "canonical_ids" : 0 , "results" : [{"message_id" : "0:1374826298092960%978fee92f9fd7ecd"}]}
The Problem:
I receive nothing on my device
What I did:
I try to do two versions of the application:
First I made an app using a part of the code of the tutorial, but with using the package com.google.android.gms.gcm.GoogleCloudMessaging instead of the com.google.android.gcm which are deprecated (and used in the tutorial), but I was unable to receive a message, so I try a second version …
This time I took the entire tutorial and just change the register function on the server to use mine, but like the first app, I receive nothing on my device. (I did this to try to understand how work the reception but it did not help me, and now I will forget this way)
Where do I need your help:
I need some/more explanation how to receive the message sent by my server.
My main activity uses the code describe on the android developer web site.
I create a broadcast class using the code on the android developer web site.
I don’t create any IntentService or service, maybe it is my error, but according what I read I understood that I don’t need one, like before with the deprecated GCMBaseIntentService
To conclude:
I really will appreciate some help to understand what I need to receive the message on my device, because actually I don’t know where I can found the information I need to be able to use this system.
Thanks.
PS: if you need some part of my code , just ask me.
EDIT
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.pushnotifications2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidhive.pushnotifications2.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Main activity. -->
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<!-- Register Activity -->
<activity
android:name="com.androidhive.pushnotifications2.cop.RegisterActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main Activity -->
<activity
android:name="com.androidhive.pushnotifications2.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name" >
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
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="com.androidhive.pushnotifications2" />
</intent-filter>
</receiver>
</application>
</manifest>
And my GcmBroadcastReceiver (who come from the tutorial)
the WakeLocker is the same as the tutorial too.
package com.androidhive.pushnotifications2;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class GcmBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
#Override
public void onReceive(Context context, Intent intent) {
///
WakeLocker.acquire(context);
///
System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
intent.getExtras().toString());
} else {
sendNotification("Received: " + intent.getExtras().toString());
}
WakeLocker.release();
setResultCode(Activity.RESULT_OK);
}
// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.common_signin_btn_icon_focus_light)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}

Categories

Resources