Can't make a toast with a broadcastreceiver, never call onReceive - java

My question will be seem's stupid but I make a simple code to make a Toast when I receive a SMS, but impossible to see my toast.
My Android Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.romain.assistantvocal" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<receiver class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".MyActivity"
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 class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
And here my class SMSReceiver :
package com.example.romain.assistantvocal;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
import java.util.Locale;
/**
* Created by romain on 26/09/2014.
*/
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"gg romstan",Toast.LENGTH_LONG).show();
}
}
Thank you in advance for your answers.

Try using this revicer:
<receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true">
<intent-filter android:priority="6565" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

replace
<receiver class="com.example.romain.SMSReceiver" android:name=".SMSReceiver">
with
<receiver android:name="com.example.romain.assistantvocal.SMSReceiver">
If ur aware of Ordered broadcasts then it says "As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. "
So. If any broadcast has stopped passing data, u won't get the message.
Try putting log in ur receiver and check. Whether ur receive message or not because ur toast message is correct.
U can't keep priority more than 1000 as per android guidelines. so, give some thing near to 1000 or remove priority and check.

Related

Broadcast Receiver doesnt work statically

I want to notify when power of the phone connects and disconnects by broadcast receiver.
but the problem is that it doesnt work when i define receiver in manifest, but it works well if i defineit dynamically.
This is my manifest.xml:
<?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.batterymanager">
<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>
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
</application>
</manifest>
And here is my Broadcast:
package com.example.batterymanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/*if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED")) {
Toast.makeText(context, "Power Connected", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals("android.intent.action.ACTION_POWER_DISCONNECTED")){
Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
}*/
Toast.makeText(context, "" + intent.getAction(), Toast.LENGTH_SHORT).show();
Log.i("HUU", intent.getAction());
}
}
I dont know why it doesnt work! ):
New android limitations:
As part of the Android 8.0 (API level 26) Background Execution Limits,
apps that target the API level 26 or higher can no longer register
broadcast receivers for implicit broadcasts in their manifest.
However, several broadcasts are currently exempted from these
limitations. Apps can continue to register listeners for the following
broadcasts, no matter what API level the apps target.
https://developer.android.com/guide/components/broadcast-exceptions

Phone State Receiver Not working in Oppo

I created a basic app to check whether broadcast is received when phone state is changed, but it is not working. I tried with making outgoing call and incoming calls too. I realized these are common issues with Vivo, Honor etc. Here is Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jss.servicetester">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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=".ReceiverMe">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
And here is code for receiver:
package jss.servicetester;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by DELL WORLD on 5/28/2017.
*/
public class ReceiverMe extends BroadcastReceiver {
String LOGTAG = "Receiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOGTAG, "action:" + intent.getAction());
}
}
Can anyone help me.
started working in oppo F3 after making some changes in battery options for application. Like Unfreeze app in background.

GcmListenerService client side notification

I am trying to use gcm to push notifications on android devices. So far I have registered with gcm and sent my back-end the necessary information to make a post request to the gcm severs which builds a 200 response. All the steps have come together except the client side receiving the message. I don't get the message. Displayed here is my gcm listener class:
import com.google.android.gms.gcm.GcmListenerService;
public class MyGcmListenerService extends GcmListenerService {
public static final int MESSAGE_NOTIFICATION_ID = 435345;
private static final String TAG = "MyGcmListenerService";
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(from,message);
}
private void sendNotification(String title, String body) {
Context context = getBaseContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title)
.setContentText(body);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
}
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<recieve android:name = ".GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.caesar.gcm" />
</intent-filter>
</recieve>
</application>
// new manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<reciever
android:name = "com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.example.caesar.gcm" />
</intent-filter>
</reciever>
<service
android:name="com.example.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
</application>
Actually looking at this further it looks like you haven't setup the receiver correctly. Seems you need to include at least.
<service
android:name="com.example.MyGcmListenerService "
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
And for the rest just follow that link.
You have to put your resgistration intent service at the end of yout Manifest file. Like this example:
<service
android:name="com.example.gcm.RegistrationIntentService"
android:exported="false">
</service>
Hope that help you, more information, you can visit this link and you can view an example of RegistrationIntentService here.

Parse.initialize crashes when opening

I've been trying to work with Parse for a while and I'm having a simple problem. I believe I am missing something with syntax but I could be wrong.
Every time I run the line,
Parse.initialize(this, "APP_ID", "CLIENT_ID");
I may have those in the wrong order. But on my system they are correct.
The application opens then closes immediately. When I moved the code, it crashes on that window.
What am I missing?
Here is my pointApp.java file:
import android.app.Application;
import android.util.Log;
import com.parse.Parse;
public class pointApp extends Application {
public void onCreate(){
super.onCreate();
Log.w("CONTENT:","OPENING...");
try {
Parse.enableLocalDatastore(this);
Parse.initialize(this, "APP_ID", "CLIENT_ID");
}catch(Exception e){
//Log.v("Error:", "Unable to connect");
}
}
}
On my activity page, launch.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class launch extends Activity{
protected void onCreate(Bundle savedInstanceState){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//here check if the gps is on.
setContentView(R.layout.gps_check);
/*THIS SHOULD REMOVE BECASE THEY SHOULDNT BE ABLE TO SKIP THE GPS SCREEN*/
Button continue_btn = (Button) findViewById(R.id.continue_btn);
continue_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.intent.action.start"));
}
});
/*END OF CODE THAT GETS REMOVE */
}
}
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.-.point">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme"
android:name=".pointApp">
<activity
android:name=".launch"
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:name=".start"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.start"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".login"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.login"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".signup"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.signup"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".friends_list"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.friends_list"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".add_friend"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.add_friend"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".point"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.point"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
You need to substitute in your app id and client id. It's on the Parse dashboard.
perhaps your jar file is broken.
please check your lib jar files.
I saw such error in my project.
When I exchange lib jar files, the error disappeared.
Please try this way.

BroadcastReceiver registered in AndroidManifest not working

Current Behavior:
The messages are being broadcast, but the receiver never gets them. I know this because the print statement in the broadcaster is printed, but the print statement in the receiver is never printed. What am I missing/doing incorrectly?
Here's the snippet where I tried (multiple ways) to broadcast a message, which is inside a class called IMService that extends Service:
//NEW_FRIEND_REQUEST is a String
Log.i( "MY_TAG", "broadcasting received friend request");
Intent i = new Intent( NEW_FRIEND_REQUEST, null, null, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( NEW_FRIEND_REQUEST );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
i = new Intent( this, NotificationReceiver.class );
i.putExtra( USER_ID, user.username() );
sendBroadcast(i);
The broadcast receiver (which is a standalone class aka not an inner class):
public class NotificationReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.i("MY_TAG", "friends broadcast receiver received a friend request message");
}
}
AndroidManifest.xml:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="NotificationsReceiver" >
<intent-filter>
<action android:name="IMService.NEW_FRIEND_REQUEST" />
</intent-filter>
</receiver>
<activity
android:name="activities.MainActivity"
android:configChanges="orientation|screenSize"
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:name="activities.Login"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name="activities.Register"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_register" >
</activity>
<activity
android:name=".HttpRequest"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_signin" >
</activity>
<activity
android:name=".Messaging"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_messaging" >
</activity>
<service android:name=".IMService" />
<activity
android:name=".FriendList"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friend_list" >
</activity>
<activity
android:name="activities.Home"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_home"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.Friends"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friends"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.FriendRequests"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_friend_requests" >
</activity>
<activity
android:name="activities.PrivateChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_private_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.RandomChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_random_chat" >
</activity>
<activity
android:name="activities.Groups"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_groups" >
</activity>
<activity
android:name="activities.GroupChat"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_group_chat"
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
<activity
android:name="activities.UserProfile"
android:configChanges="orientation|screenSize"
android:label="#string/title_activity_user_profile" >
</activity>
<activity
android:name=".Messages"
android:label="#string/title_activity_messages" >
</activity>
</application>
</manifest>
EDIT:
After reading Mike's comment, I tried the following 2 things (separately), but the receiver still does not receive the message:
<!-- no filter -->
<receiver android:name="NotificationReceiver" />
and:
<!-- with a filter, and I changed the value of NEW_FRIEND_REQUEST to
public static final String NEW_FRIEND_REQUEST = "new_friend_request"; -->
<receiver android:name="NotificationReceiver" >
<intent-filter>
<action android:name="new_friend_request" />
</intent-filter>
</receiver>
Here is my project's hierarchy:
Last Edit:
For anyone curious about the exact warning when I used a capital letter for the package name, here's a screen shot:
When using implicit Intents - i.e., Intents not created for a specific class - the action you're filtering for on the BroadcastReceiver must exactly match that which you use to create the Intent. In this case, the NEW_FRIEND_REQUEST String specified in the code did not match the <action> element in the manifest.
Also, when a Receiver is incorrectly listed in the manifest, firing an implicit Intent meant for it will fail silently, as the system won't be able find it. In this case, an extra s in NotificationReceiver was one problem. Another was that the Receiver is located in a subdirectory of /src, and therefore in a different package. This was corrected by changing the name listed in the manifest to include the subdirectory in the package name.
Finally, it appears that capital letters in package names - and, therefore, project directory names - can cause problems, and should be avoided.

Categories

Resources