Android - Custom background image - android

I want to change my app's background randomly. It's supposed to have a random image from its own folder as background. It works perfectly with some devices but with some of them doesn't. I wonder what could cause this inconsistency. Here is my random background generator class:
public class arkaplanGenerator {
public static int arkaplan(){
int sonucArkaplan = 0;
Random r = new Random();
int sayi = r.nextInt(11);
switch (sayi){
case 0:
sonucArkaplan = R.drawable.bavaria;
break;
case 1:
sonucArkaplan = R.drawable.gorges;
break;
case 2:
sonucArkaplan = R.drawable.more;
break;
case 3:
sonucArkaplan = R.drawable.mountains;
break;
case 4:
sonucArkaplan = R.drawable.pisa;
break;
case 5:
sonucArkaplan = R.drawable.sea;
break;
case 6:
sonucArkaplan = R.drawable.sunset;
break;
case 7:
sonucArkaplan = R.drawable.sunset2;
break;
case 8:
sonucArkaplan = R.drawable.wai;
break;
case 9:
sonucArkaplan = R.drawable.water;
break;
case 10:
sonucArkaplan = R.drawable.waterfall;
break;
}
return sonucArkaplan;
}
And here is how i use it in my main activity:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.activity_main);
linearLayout.setBackground(getResources().getDrawable(arkaplanGenerator.arkaplan()));
Those images are in drawable folder.Thanks in advance.

The method getDrawable from the Resources class has been deprecated. You should use the one from ContextCompat:
linearLayout.setBackground(ContextCompat.getDrawable(getApplicationContext(),arkaplanGenerator.arkaplan()));

Related

how much is the number of SMS sending with SMSManager?

I would like to know how much is the sending of an SMS via SMSManager? let me explain:
I have a list of several people and I make a FOREACH loop on their phone number.
The list contains more than 200 numbers
Thanks
after going through the Android sources for all the APIs supported by my app I cam up with the following code
`private void setSmsDefaultLimitations(){
int apiLevel = Build.VERSION.SDK_INT;
String versionRelease = Build.VERSION.RELEASE;
switch(apiLevel){
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
sMaxAllowed = 100;
sCheckPeriod = 3600000;
break;
case 16:
sMaxAllowed = 30;
sCheckPeriod = 1800000;
break;
case 17:
sMaxAllowed = 30;
if(versionRelease.contains("4.2.2")){
sCheckPeriod = 60000;
}else {
sCheckPeriod = 1800000;
}
break;
case 18:
sMaxAllowed = 30;
sCheckPeriod = 60000;
break;
default:
sMaxAllowed = 30;
sCheckPeriod = 1800000;
break;
}
sMaxAllowed = sMaxAllowed - 2; //This is to give us a little buffer to be extra safe (like a condom ;)
Log.d(TAG, "maxAllowed = "+sMaxAllowed+"; checkPeriod = "+(sCheckPeriod/60000) + " minutes");
}`

I cannot call getColor method using ContextCompat object. It wont let me pass getContext() as its first parameter

So this is what I am doing.The below statement is the last line of an int return type method.
return ContextCompat.getColor(getContext(),magColor);
The error says that i have passed the wrong first argument.
Precisely:
Wrong 1st argument type. Found: 'java.security.AccessControlContext',
required: 'android.content.Context'
Entire Method:
public int getMagnitudeColor(double mag) {
int magColor;
int mag1=(int) mag;
switch (mag1) {
case 1:
magColor = R.color.magnitude1;
break;
case 2:
magColor = R.color.magnitude2;
break;
case 3:
magColor = R.color.magnitude3;
break;
case 4:
magColor = R.color.magnitude4;
;
break;
case 5:
magColor = R.color.magnitude5;
break;
case 6:
magColor = R.color.magnitude6;
break;
case 7:
magColor = R.color.magnitude7;
break;
case 8:
magColor = R.color.magnitude8;
break;
case 9:
magColor = R.color.magnitude9;
break;
default:
magColor = R.color.magnitude10plus;
break;
}
return ContextCompat.getColor(getContext(),magColor);
}
This is because your getContext() in:
return ContextCompat.getColor(getContext(),magColor);
is not android.content.Context.
You need to use context from Activity, Service, Application and View.
If you use custom class, you need to pass the context to the class.
I did this and it worked , 1 single line
view.listViewPalabrasLinearLayout.setBackgroundColor(ContextCompat.getColor(view.context,color))
where "listViewPalabrasLinearLayout" is the id of my layout and "color" is the variable I specify the color

Unreachable code after switch statement

public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1,String[] paramArrayOfString2, String paramString2)
{
SQLiteQueryBuilder localSQLiteQueryBuilder = new SQLiteQueryBuilder();
if (paramUri.getPathSegments().size() == 1);
for (StringBuilder localStringBuilder = null; ; localStringBuilder = new StringBuilder(100))
switch (sURIMatcher.match(paramUri))
{
case 0:
case 1:
case 2:
case 3:
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
localSQLiteQueryBuilder.setTables("category");//unreachable code
while (true)
{
Cursor localCursor = localSQLiteQueryBuilder.query(mOpenHelper.getReadableDatabase(), paramArrayOfString1, paramString1, paramArrayOfString2, null, null, paramString2);
localCursor.setNotificationUri(contentResolver, paramUri);
return localCursor;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.appendWhere("shop_category_id=category._id");
continue;
localSQLiteQueryBuilder.setTables("shop,category");
StringBuilder localStringBuilder;
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
continue;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.setDistinct(true);
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("shop_category_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
paramString2 = "shop._id";
}
}
I get unreachable code error after switch statement and I can't figure it out how to solve it.I tried to delete that line but if I do I get a lot of errors.My code is above.Can anyone help me?Thanks in advance.
The code is really unreachable:
All the cases are fall-through (they have no break statement, so all cases after a match will execute) and end in the default case which throws an Exception. This means the code after throwing the Exception will never be executed.
Perhaps what you meant to do was rahter sth like this:
switch (sURIMatcher.match(paramUri)){
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
I guess it is due to your bad switch...you must use break;
switch (sURIMatcher.match(paramUri))
{
case 0:
//your code
break;
case 1:
//your code
break;
case 2:
//your code
break;
case 3:
//your code
break;
default:
//your code
break;
}

switch\case not working as expected

My Log line says animalclass is 4 but instead of going to "case 4" it returns default. It works with case 2 though. How is this possible? Thanks in advance.
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
case 4: //
tile=R.drawable.picnictile;
case 5: //
tile=R.drawable.face;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
you need to use break; to stop other case execution because without break it will execute the correct case block statements and also "default" code block
Try adding the Break statemenet after all cases.
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
If you dont break it after the "thing" the case should do, the switch does not work correct.
You forgot to add a break keyword before the default keyword.
Try this one:
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
Add break; keyword after each case, otherwise switch will execute default statement also and result will be unexpected.

Android TelephonyManager.getNetworkType() returned constant values in bearer speed order?

TelephonyManager.getNetworkType() returns one of the constant values.
It appears that the constant values have an integer order, by possible bearer link speed.
I know using constant values used in the following manner is generally bad,
however could one use this to determine a basic cutoff for application functionality and have it work between API levels? (in API-v1 there was nothing above 0x03)
if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_EDGE )
{
return "3G! party on!";
}
else if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_UNKNOWN )
{
return "2G, OK. just don't go nuts!";
}
else
{
return "No data sorry"
}
You cannot assume they are in order because they are not. For example, LTE is 13 while HSPAP (HSPA+) is 15. Those are not in order. I wrote a "speed ranking" piece of code, which assignes each network type its own speed rank
public static int getNetTypeSpeedRank(int t) {
switch (t) {
case -1:
t = -1;
case ContextManager.MDM_NETWORK_TYPE_UNKNOWN:
t = 0;
break;
case ContextManager.MDM_NETWORK_TYPE_IDEN:
t = 1;
break;
case ContextManager.MDM_NETWORK_TYPE_GPRS:
t = 2;
break;
case ContextManager.MDM_NETWORK_TYPE_EDGE:
t = 3;
break;
case ContextManager.MDM_NETWORK_TYPE_UMTS:
t = 4;
break;
case ContextManager.MDM_NETWORK_TYPE_CDMA:
t = 5;
break;
case ContextManager.MDM_NETWORK_TYPE_1xRTT:
t = 6;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_0:
t = 7;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_A:
t = 8;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_B:
t = 9;
break;
case ContextManager.MDM_NETWORK_TYPE_HSDPA:
t = 10;
break;
case ContextManager.MDM_NETWORK_TYPE_HSUPA:
t = 11;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPA:
t = 12;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPAP:
t = 13;
break;
case ContextManager.MDM_NETWORK_TYPE_EHRPD:
t = 14;
break;
case ContextManager.MDM_NETWORK_TYPE_LTE:
t = 15;
break;
default:
t = 16;
}
return t;
}
I really wouldn't count on that behavior.

Categories

Resources