Cannot find getApplicationContext() in class extended from FirebaseMessagingService - android

I want to send and receive remote notification through Firebase but my custom class could not find getApplicationContext() method.
From my understanding, FirebaseMessagingService extends Service, which extendsContextWrapper` which extends Context.
So I don't know getApplicationContext() is not working. Any help will be appreciated!
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseMessageService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle();
String text = remoteMessage.getNotification().getBody();
NotificationHelper.displayNotification(getApplicationContext(), title, text);
}
super.onMessageReceived(remoteMessage);
}
}
Here's my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mealz">
<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.NoActionBar">
<activity android:name=".Activities.UserActivity"></activity>
<activity android:name=".Activities.LoginActivity" />
<activity android:name=".Activities.SignupActivity" />
<activity android:name=".Activities.SearchRecipeActivity" />
<activity android:name=".Activities.RecipeDetailActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Notifications.FirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>

You can achieve this in many way one way is to keep a weak refrence of context make a app class extending android.app.application and on it's create method and make a variable of weak refrence of context and assign it to getApplicationContext() and you may use this context anywhere you want.
public class app extends android.app.Application{
private static WeakReference<Context> referenceCntx;
#Override
public void onCreate() {
super.onCreate();
referenceCntx = new WeakReference<>(getApplicationContext());
}
public static Context getApplicationCntx(){
return referenceCntx.get();
}
}
And don't forget to add this line in manifest
<application
android:name=".app"

Related

android detect change connection dont work

I'm wondering why this simple receiver for detecting connection change don't work for me, it is like with al other sample which i search on google
in this sample application i have only one activity and broadcast
BroadcastReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ConnectivityReceiver extends BroadcastReceiver {
public ConnectivityReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent arg1) {
Log.e("onReceive ", " , ");
}
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.sampleapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".ConnectivityReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>
after turning on/off i dont have any output on logcat
and its my activity:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
If your application is targeting Android N, then you must have to register your CONNECTIVITY_CHANGE broadcast dynamically Context.registerReceiver().
Reference:
Android N Background Optimizations

Error : MainActivity is not an enclosing class

I create a personnal component view and when we click on this, an other activity starts. There is my manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.freshkamekentrainement.skrt">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:theme="#style/Splash"
android:screenOrientation="portrait"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation"/>
<activity android:name=".niveaux.LilUziVert_GrowUp"
android:screenOrientation="portrait"
android:configChanges="orientation"/>
</application></manifest>
there is my intent
public class Niveauview extends RelativeLayout {
Intent intentNiveau;
//Code
#Override
public void onFinishInflate() {
super.onFinishInflate();
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
intentNiveau = new Intent(MainActivity.this,LilUziVert_GrowUp.class);
startActivity(intentNiveau);
}
});
}}
I get error:
MainActity is not an enclosing class
Notice that NiveauView and MainActivity isn't in the same package (but they are public). Where does the problem come from? When i try new Intent(this,LilUziVert_GrowUp.class); i have an error too.
You can only access Foo.this if you're in a inner class of Foo. Otherwise you need to be passed in an instance of Foo instead. In the case of a View, every view has an instance of a Context you can get by calling getContext(). So you need to call intentNiveau = new Intent(getContext(),LilUziVert_GrowUp.class);

BroadcastReceiver not being instantiated

I have a BroadcastReceiver that is not being instantiated or called, any help on what I'm doing is appreciated.
It should be responding to wifi connection/disconnection events but isn't, and it's superclass constructor is not even being called either.
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
BroadcastReceiver:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "action: " + intent.getAction());
Log.v(TAG, "component: " + intent.getComponent());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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:name=".ConnectivityChangeReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
The <action> name is set incorrectly. It should be: android.net.conn.CONNECTIVITY_CHANGE. See the "Constant Value" as described here.

PackageBroadcastReceiver not working after app is closed

I have a PackageBroadcastReceiver running ok while the app is in the background. I need it to keep listening to packages added/removed when it is closed. But, it is not.
Is there any way to do this with a BroadcastReceiver or should I move to a service?
Here the code:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cresan.antivirus.StockingActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.tech.applications.coretools.advertising.PackageBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
And my PackageBroadcastReceiver:
public class PackageBroadcastReceiver extends BroadcastReceiver
{
static IPackageChangesListener _listener;
static public void setPackageBroadcastListener(IPackageChangesListener listener)
{
_listener=listener;
}
#Override
public void onReceive(Context ctx, Intent intent)
{
if(_listener!=null && Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()))
_listener.OnPackageAdded(intent);
if(_listener!=null && Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()))
_listener.OnPackageRemoved(intent);
}
}
Cheers.
You will definitely need to implement a background service to do this. See the developer guide.
You can then programmatically register a BroadcastReceiver in your service:
private PackageBroadcastReceiver packageBroadcastReceiver;
#Override
public void onCreate() {
super.onCreate();
this.packageBroadcastReceiver = new PackageBroadcastReceiver();
this.packageBroadcastReceiver.setPackageBroadcastListener(new IPackageChangesListener() {
public void OnPackageAdded(Intent i) {
//do something here- probably start an activity
}
...
});
IntentFilter packageFilter = new IntentFilter("android.intent.action.PACKAGE_ADDED");
packageFilter.addAction("android.intent.action.PACKAGE_INSTALL");
packageFilter.addDataScheme("package");
this.registerReceiver(this.packageBroadcastReceiver, packageFilter);
}
#Override
public void onDestroy(){
super.onDestroy();
this.unregisterReceiver(this.packageBroadcastReceiver);
}
Try registering broadcast receiver in service. After activity is destroyed, receiver will be destroyed to so you will not get broadcast event.

Illegal access exception while sharing Global data

i want to share the global data across activities and i have followed
this link
.but i am not getting how to declare it in my manifest. i am posting my manifest code, i have tried it in different ways, but still getting the error. please tell me how to resolve it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".Myapp">
<activity android:name=".AndroidtestActivity"
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>
This is my main activity
public class AndroidtestActivity extends Activity
{
/** Called when the activity is first created. */
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText et = (EditText) findViewById(R.id.text1);
Myapp appState = ((Myapp)getApplicationContext());
String s= appState.getState();
et.setText(s);
}
}
and my Myapp class is
class Myapp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = "hello world";
}
}
i am getting error in the line
Myapp appState = ((Myapp)getApplicationContext());
illegal access exception ,please tell me how to resolve this problem
please help me with this.
Merge the application tags (i.e. put the android:name in the first one and delete the rest):
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:name=".MyApp">
<activity android:name=".AndroidtestActivity"
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>
Then use getApplication() instead of getApplicationContext().

Categories

Resources