Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Shared Preferences file path Android


AD MOB


/data/data/<package>/shared_prefs/<package>_preferences.xml

String TAG = "MyActivityName";
File f = new File("/data/data/"+this.getPackageName()
+"/shared_prefs/"+this.getPackageName()+"_preferences.xml");
if(f.exists())
	Log.d(TAG, "exist");
else
	Log.d(TAG, "not exist");


397LW NO topic_id




AD

Další témata ....(Topics)


291

This version of the rendering library is more recent than | this-version-of-the-rendering-library-is-more-recent-than


This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in

Click on menu Help > Install New Software.
In the Work with field, Add: https://dl-ssl.google.com/android/eclipse/
Select: Developer Tools / Android Development Tools.
Click Next to complete the wizard.

If you have problem try download all sdk + eclipse in one pack , rename old folder for example Andorid_old, create new folder Android and unpack sdk + eclipse from this adress:
//developer.android.com/sdk/index.html


adt-eclipse-update
232

goto statement in Java | goto-statement-in-java


You can use for, do while, while cycle for example:

public void myFunction(){
 for (int i = 0; i < 1; i++) {
	// some code
       int c = 10;
       if(c==10)
         break; // goto stop; in C++
 } // end of for
 // stop: // break moved process to end of for
 


 // next code

}


108

Turn screen ON OFF Android sample code | turn-screen-on-off-android-sample-code


WakeLock, PowerManager,uses-permission Android sample.
Main.java

public class Main extends Activity {
	    private SensorManager mSensorManager;
	    private PowerManager mPowerManager;
	    private WindowManager mWindowManager;
	    private WakeLock mWakeLock;
	    private Button button;
	    private TextView textView;

	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
try{
	        // Get an instance of the SensorManager
	        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

	        // Get an instance of the PowerManager
	        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);

	        // Get an instance of the WindowManager
	        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
	        mWindowManager.getDefaultDisplay();

	        // Create a bright wake lock
	        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass()
	                .getName());

	        setContentView(R.layout.main);
	        textView = (TextView)findViewById(R.id.textView1);
	        button = (Button)findViewById(R.id.button1);
	        button.setOnClickListener(mButtonStopListener);
	       
	        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("onCreate", e.getMessage());
		}
} // END onCreate
	    
	    View.OnClickListener mButtonStopListener = new OnClickListener() {
	    	public void onClick(View v) {
		        try {
					mWakeLock.release();
					textView.setText("mWakeLock.release()");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Log.e("onPause",e.getMessage());
				}
	    	
	    	}
	    };

	    @Override
	    protected void onResume() {
	        super.onResume();
	        /*
	         * when the activity is resumed, we acquire a wake-lock so that the
	         * screen stays on, since the user will likely not be fiddling with the
	         * screen or buttons.
	         */
	        
	        try {
				mWakeLock.acquire();
				textView.setText("mWakeLock.acquire()");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("onResume", e.getMessage());
			}

	    }

	    @Override
	    protected void onPause() {
	        super.onPause();

	        // and release our wake-lock
	        try {
				mWakeLock.release();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("onPause",e.getMessage());
			}
	    }
}



AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="//schemas.android.com/apk/res/android"
      package="cz.okhelp.Main"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ScreenBrightnessActivity"
                  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>


219

SparseArray instead HashMap Android example | sparsearray-instead-hashmap-android-example


Old code with HashMap

 Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
   private void fillBitmapCache() {
        _bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
        _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
        _bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
        _bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(), 
    }

Bitmap bm = _bitmapCache.get(R.drawable.icon);

Lint warning:
Use new SparseArray(...) instead for better performance
Issue: Looks for opportunities to replace HashMaps with the more efficient SparseArray
Id: UseSparseArrays

New code with SparseArray

 SparseArray<Bitmap> _bitmapCache = new SparseArray<Bitmap>();
   private void fillBitmapCache() {
        _bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
        _bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
        _bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
        _bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(), 
    }

Bitmap bm = _bitmapCache.get(R.drawable.icon);


SparseArray methods:
//developer.android.com/reference/android/util/SparseArray.html
313

Difference between logical operators and or Java | difference-between-logical-operators-and-or-java


Basic difference remember it!!!
if(TRUE && TRUE && TRUE) return TRUE otherwise FALSE

if(FALSE || FALSE || FALSE) return FALSE otherwise TRUE

Logical operator and &&
If all conditions/operands is TRUE return TRUE, otherwise return FALSE

if( true and true and true){ 
  // return true - do something
}
int a = 6;
if(a == 6 && a == 6 ) {
 // if TRUE
 // true && true return true, do something
}

if(a == 6 && a == 5){
  // nothing, not attended
}else{
  // true && false return false, or false && false return false 
  // do something
}


Logical operator or ||
The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise.
If one operands is TRUE, condition is TRUE:

if(FALSE OR FALSE OR TRUE) return TRUE
if(FALSE OR TRUE OR FALSE) return TRUE
if(FALSE OR FALSE OR FALSE) return FALSE

int a = 6;
if(a==6 || a==5){ // TRUE || FALSE return TRUE
  //if return TRUE
  //one from operadns is TRUE return true,  do something
}

if(a==5 || a==4){ // FALSE || FALSE return FALSE
 // not attended
}else{
 //if return FALSE, do something
}

rev


Editace: 2013-11-11 19:01:56
Počet článků v kategorii: 397
Url:shared-preferences-file-path-android

AD
11 Share
Share
Tweet