TheCocoaProject / cordova-plugin-nativestorage

Cordova plugin: Native storage of variables in Android, iOS and Windows
http://thecocoaproject.github.io/
Apache License 2.0
292 stars 106 forks source link

GetNativeStorage from Java #52

Closed appzer closed 7 years ago

appzer commented 7 years ago

Hello,

can someone help me! I save some data with this plugin. Now i want to read out the data from Java. I created a widget and want use the stored data!

This ist the widget java source:

package de.appzer.Pushsafer;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;
import android.widget.Toast;

import static android.content.Context.MODE_PRIVATE;
import static de.appzer.Pushsafer.R.layout.pushsafer_widget;

/**
 * Implementation of App Widget functionality.
 */
public class PushsaferWidget extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), pushsafer_widget);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }

        for(int j = 0; j < appWidgetIds.length; j++)
        {
            int appWidgetId = appWidgetIds[j];

            try {
                Intent intent = new Intent("android.intent.action.MAIN");
                intent.addCategory("android.intent.category.LAUNCHER");

                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName()));
                PendingIntent pendingIntent = PendingIntent.getActivity(
                        context, 0, intent, 0);
                RemoteViews views = new RemoteViews(context.getPackageName(), pushsafer_widget);
                views.setOnClickPendingIntent(R.id.button, pendingIntent);
                appWidgetManager.updateAppWidget(appWidgetId, views);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context.getApplicationContext(),
                        "There was a problem loading the application: ",
                        Toast.LENGTH_SHORT).show();
            }

        }

        SharedPreferences sharedPreferences = context.getSharedPreferences("MainActivity", MODE_PRIVATE);
        System.out.println("********---------    shared pref values...   " +  sharedPreferences.getString("native-messages", "no value"));

    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

This code do nothing

SharedPreferences sharedPreferences = context.getSharedPreferences("MainActivity", MODE_PRIVATE);
        System.out.println("********---------    shared pref values...   " +  sharedPreferences.getString("native-messages", "no value"));

native-messages = is the stored var

Thanks kevin

GillesC commented 7 years ago

Hey Kevin

First of all, this kind of questions are better suited on StackOverflow. Consider using our tag (cordova-nativestorage) when posting questions related to our plugin.

Second, have you read our documentation? There I've provided a link to the same question which you're asking now.

So if you will inspect the code I've provided on StackOverflow, you will see that your PREFS_NAME is not correct. You are using "MainActivity" instead of the used name in the NativeStorage plugin, which is "NativeStorage". Please consider giving my answer an upvote, so other people can find this answer.