I'm trying to get some data from Word Readable Shared Preferences of another application.
I'm sure that other application Shared Preferences is World Readable and name is present.
But I'm retrieving empty string.
Here is my code:
package com.example.sharedpref;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.SharedPreferences;
public class MainActivity extends AppCompatActivity {
private TextView appContent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContent = (TextView) findViewById(R.id.test1);
appContent.setText("Test Application\n");
appContent.setText(getName(this));
}
protected String getName(Context context){
Context packageContext = null;
try {
packageContext = context.createPackageContext("com.application", CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = packageContext.getSharedPreferences("name_conf", 0);
String name = sharedPreferences.getString("name", "");
return name; //HERE IS WHERE I PUT BREAKPOINT
}
catch (PackageManager.NameNotFoundException e) {
return null;
}
}
}
Here is some debug I retrieve from debug mode:
and piece of code from the app that I'm trying to access shared preferences:
/* access modifiers changed from: protected */
#SuppressLint({"WorldReadableFiles"})
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_welcome);
Editor edit = getApplicationContext().getSharedPreferences("name_conf", 0).edit();
edit.putBoolean("FIRSTRUN", false);
edit.putString("name", "Eddie");
edit.commit();
new DBHelper(this).getReadableDatabase();
PS. I Cant edit code of second app im trying access to!
Something im missing?
You can not edit it from another apps,
if you want, you can use FileWriter to create a txt file and use it in other apps
Related
I am working on my first app (so please forgive me if I've made a silly mistake, I am relatively new to coding). I created a settings activity for my app, but the moment I click on the settings button from the action bar, my app crashes. I tried debugging the app, turns out the moment I remove the lines where I keep the switches on setchecked, it works fine, but then if I remove the app from memory and open it again, the setting isnt saved, the switches aren't on. Please help.
package com.example.taskmasterv3;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Switch;
public class SettingsActivity extends AppCompatActivity {
public static final String SETTINGS_PREFERENCES = "com.example.taskmasterv3.SettingsPreferences";
Switch switchReminder, switchNotifications;
boolean reminders;
boolean notifications;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE);
reminders = prefs.getBoolean("reminders", false);
notifications = prefs.getBoolean("notifications", false);
switchReminder.setChecked(reminders);
switchNotifications.setChecked(notifications);
switchReminder = findViewById(R.id.switchReminder);
switchNotifications = findViewById(R.id.switchNotifications);
if (switchReminder.isChecked()) {
reminders = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("reminders", reminders);
editor.commit();
}
if (switchNotifications.isChecked()) {
notifications = true;
SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_PREFERENCES, MODE_PRIVATE).edit();
editor.putBoolean("notifications", notifications);
editor.commit();
}
}
}
You're not using findViewById to retrieve your switches in onCreate before you're using them, so they're null:
switchReminder.setChecked(reminders);
switchNotifications.setChecked(notifications); <-- wrong order
switchReminder = findViewById(R.id.switchReminder); <-- too late, already tried to use it above
switchNotifications = findViewById(R.id.switchNotifications);
it should be:
switchReminder = findViewById(R.id.switchReminder);
switchNotifications = findViewById(R.id.switchNotifications);
switchReminder.setChecked(reminders);
switchNotifications.setChecked(notifications);
package comviewappisome.google.sites.watchandearn;
public class SharedPreferences {
public static final String PREFS_NAME = "MyLoginPrefsFile";
}
package comviewappisome.google.sites.watchandearn;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import static comviewappisome.google.sites.watchandearn.SharedPreferences.PREFS_NAME;
public class login extends AppCompatActivity {
LinearLayout guestlogin = (LinearLayout )findViewById(R.id.guest);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
guestlogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences settings = getSharedPreferences(comviewappisome.google.sites.watchandearn.SharedPreferences.PREFS_NAME, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();
//Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
// Commit the edits!
editor.commit();
Intent i = new Intent(login.this, ChoiceSelection.class);
startActivity(i);
}
});
}
}
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
SharedPreferences settings = getSharedPreferences(comviewappisome.google.sites.watchandearn.SharedPreferences.PREFS_NAME, 0);
//Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if(hasLoggedIn)
{
Intent i = new Intent(SplashScreen.this, login.class);
this.startActivity(i);
this.finish();
}
else
{
Intent n = new Intent(SplashScreen.this, ChoiceSelection.class);
this.startActivity(n);
this.finish();
}
}
}
This, I code in javafile and then this error show me while debug.I delete this files also and set file to previously, but I did not get any solution. My build.gradle and main.xml is also right and there is error showing while gradle sync only in debuging.I try Restart and Invalides Caches
I think this problem is caused by other classes. If you are using a web service, the json format returned from there is the wrong format.
Your client expects an array (or JSON array) but it gets an string (starts with '"') . check your string and find out the difference between. maybe it needs to remove quotation marks from start and end. but to be honest, you must LOG them to get a proper solution.
I fixed the problem by cleaning the build!
Hi I'm currently trying to save a simple integer value from an EditText to sharedPreferences so that I can access it within any activity. I've tried following the google tutorials but I was unable to make it work. I ended up getting the error message "Cannot resolve method 'getPreferences' ". I realize that there have been other threads about sharedPreferences but I cannot seem to make sense of them.
Also, if you can help me find a better way than my catch statement to avoid the NumberFormatException, please let me know. Thanks again!Any help is much appreciated!
Java
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.managergmail.time.finite.finitemanager02.R.id.textViewTest;
public class ExamPrepHome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_prep_home);
Button buttonSaveNumberOfExams = (Button) findViewById(R.id.buttonSaveNumberOfExams);
try{
final EditText numberOfExamsInput = (EditText) findViewById(R.id.numberOfExamsInput);
final int numberOfExamsValue = Integer.valueOf(numberOfExamsInput.getText().toString());
buttonSaveNumberOfExams.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_number_of_exams), numberOfExamsValue);
editor.commit();
}
});
}catch(NumberFormatException ex){
System.out.println("Value at TextView is not a valid integer");
}
}
}
Instead of
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
use SharedPreferences sharedPref = ExamPrepHome.this.getPreferences(Context.MODE_PRIVATE);
Reason for this is because inside the anonymous class, 'this' represents that inner class and not the Activity object. And you have to use number format exception to check to avoid crashes when you retrieve the object.
I'm creating an app that I want to stream my foscam live feed in. I'm pretty new to coding and some of this code is over my head. I found some help getting this far but now am hitting a snag. The app runs but only displays a black screen. I believe i have the manifest and XML code all correct. The problem lies in my code. I hope someone can help me out
package com.rednak.camerastream;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity
implements MediaPlayer.OnPreparedListener,
SurfaceHolder.Callback {
final static String USERNAME = "guest";
final static String PASSWORD = "Guest";
final static String RTSP_URL = "rtsp://http://rednak71.ddns.net:8090/live1.sdp";
private MediaPlayer _mediaPlayer;
private SurfaceHolder _surfaceHolder;
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up a full-screen black window.
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setBackgroundDrawableResource(android.R.color.black);
setContentView(R.layout.activity_main);
// Configure the view that renders live video.
SurfaceView surfaceView =
(SurfaceView) findViewById(R.id.surfaceView);
_surfaceHolder = surfaceView.getHolder();
_surfaceHolder.addCallback(this);
_surfaceHolder.setFixedSize(320, 240);
}
// More to come…
/*
SurfaceHolder.Callback
*/
#
Override
public void surfaceChanged(
SurfaceHolder sh, int f, int w, int h) {}
#
Override
public void surfaceCreated(SurfaceHolder sh) {
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setDisplay(_surfaceHolder);
Context context = getApplicationContext();
Map headers = getRtspHeaders();
Uri source = Uri.parse(RTSP_URL);
try {
// Specify the IP camera’s URL and auth headers.
_mediaPlayer.setDataSource(context, source, headers);
// Begin the process of setting up a video stream.
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepareAsync();
} catch (Exception e) {}
}
#
Override
public void surfaceDestroyed(SurfaceHolder sh) {
_mediaPlayer.release();
}
private Map getRtspHeaders() {
Map headers = new HashMap();
String basicAuthValue = getBasicAuthValue(USERNAME, PASSWORD);
headers.put("Authorization", basicAuthValue);
return headers;
}
private String getBasicAuthValue(String usr, String pwd) {
String credentials = usr + ":" + pwd;
int flags = Base64.URL_SAFE | Base64.NO_WRAP;
byte[] bytes = credentials.getBytes();
return "Basic" + Base64.encodeToString(bytes, flags);
}
/*
MediaPlayer.OnPreparedListener
*/
#
Override
public void onPrepared(MediaPlayer mp) {
_mediaPlayer.start();
}
}
Make sure that Android's MediaPlayer can actually open and decode your stream. Right now, if the MediaPlayer cannot handle your stream, you are catching any exception and silently ignoring it:
try {
// Specify the IP camera’s URL and auth headers.
_mediaPlayer.setDataSource(context, source, headers);
// Begin the process of setting up a video stream.
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.prepareAsync();
} catch (Exception e) {}
At the very least you should log the error:
} catch (Exception e) {
Log.e("MyApp", "Could not open data source", e);
}
Although the MediaPlayer service will most likely pepper the log with its own errors. So what you should do is review the logcat for any messages from the "VideoDecoder" or similar.
To see the logcat in Android Studio, open the "Android Monitor" tab which is on the bottom by default. If you want to see the unfiltered logcat make sure that in the top-right corner of the Android Monitor view it says "No Filters" instead of "Show only selected application".
I have some new code that links to the Foscam videostream but only grabs the frame when it starts then does not stream. Im closer but still need help. Am i on the right track here?
package com.rednak.camstream;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.VideoView;
public class MainCamActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_cam);
VideoView vidView = (VideoView)findViewById(R.id.CamVideoView);
String vidAddress = "http://rednak71.ddns.net:8090/CGIProxy.fcgi? cmd=snapPicture2&usr=guest&pwd=guest&t=";
Uri vidUri = Uri.parse(vidAddress);
vidView.setVideoURI(vidUri);
vidView.start();
}
}
I'm working on an app and would like to save the state of obe of my checkbox options, but when I try using the SharedPreferences class (first time using it) I get a nullpointer exception. I have been troubleshooting for a couple hours and cant find a solution. Could anyone look at this and tell me what's wrong? The full code is alot longer but this is the park giving me the null pointer exception. I know for a fact it has to do with SharedPreferences.
package us.mattmccoy.meanfind;
//import java.util.Arrays;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
//normal private data
final Context con1 = this;
int dividend;String dataFixed;boolean tb;
private CheckBox myCBox, acBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
}
//check box listeners
public void addListenerBoxes(){
//instantiate boxes
acBox = (CheckBox) findViewById(R.id.chex2);
acBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is acBox checked?
if (((CheckBox) v).isChecked()) {
edit.putBoolean(autoclearKEY, true).commit();
}else{
edit.putBoolean(autoclearKEY, false).commit();
}
}
});
myCBox = (CheckBox) findViewById(R.id.chex);
}
error message:
http://pastebin.com/5P2Mfwik
Change this line
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
to
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
and put
preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
in onCreate()
You are trying to use Context before establishing it with onCreate() so you are getting NPE on context. You will also want to move these lines
Editor edit = preferences.edit();
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
into onCreate() after you initialize preferences or you will get another NPE since preferences will be null until you initialize it. So it should be something like
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences; //declare them
Editor edit;
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit(); //initialize them
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
If this doesn't solve your problem then please post full logcat