首页 > 编程技术 > android

Android判断当前屏幕是全屏还是非全屏

发布时间:2016-9-20 19:59

在安卓开发时我碰到一个问题就是需要实现全屏,但又需要我们来判断出用户是使用了全屏或非全屏了,下面我分别找了两段代码,大家可参考。

先来看一个android屏幕全屏实现代码

 代码如下 复制代码

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.newslists);
newsListLayout = findViewById(R.id.newslistlayout);
newsListLayout.setBackgroundColor(Color.MAGENTA);

newsNameList = (ListView) findViewById(R.id.newsnamelist);
model = new Model(0, 6);
nameListAdapter = new NewsNameListAdapter(this, model);
newsNameList.setAdapter(nameListAdapter);

showPage = (TextView) findViewById(R.id.newslistshowpage);
updatePage(model.getIndex());
}

现在我们还可以来判断是全屏或非全屏

 代码如下 复制代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
       
        int v = this.getWindow().getAttributes().flags;
        // 全屏 66816 - 非全屏 65792
        if(v != 66816){//非全屏
         this.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }else{//取消全屏
         this.getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

    }  

本文章来给大家介绍android检测包名和类名是否存在的方法,有需要了解的同学可进入参考参考。

1.对包名的判断,异常则说明不存在:

 代码如下 复制代码


  try { 
    PackageManager pm = getPackageManager();
    pm.getPackageInfo("com.org", PackageManager.GET_ACTIVITIES);
  } catch (NameNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }


2.对类名的判断,异常则说明不存在:

 代码如下 复制代码


  try {
  Class.forName("com.org.MainActivity");
} catch (ClassNotFoundException e) {
  // TODO Auto-generated catch block
    return;
}

本文章来给大家但要在Android开发中实现获取本机电话号码的实现,这里我们必须是通过SDK来获取哦,有需要了解的朋友可参考。

注:根据Android的安全机制,在使用TelephonyManager时,必须在AndroidManifest.xml中添加
name="READ_PHONE_STATE" /> 否则无法获得系统的许可。

Android开发平台中,可通过TelephonyManager 获取本机号码。

 代码如下 复制代码

private String getPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}

或这样写

 代码如下 复制代码
TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
txtPhoneNumber.setText(phoneMgr.getLine1Number()); //txtPhoneNumber

是一个EditText 用于显示手机号

本文章来给大家介绍两段Android实现图片循环播放程序代码,有需要了解的朋友可进入参考。

SDK后个不错的实例

 代码如下 复制代码

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleWithFixedDelay(new runner(), 0, 1, TimeUnit.SECONDS);
//或者用scheduler.scheduleAtFixedRate(new runner(),0,1, TimeUnit.SECONDS);

//接着我们要实现Runnable方法,也就是准时变动现在播放图片的ID

public class runner implements Runnable
{
public void run()

  {

// TODO Auto-generated method stub
currIndex = (currIndex+1)%bitmapId.length;
bofang.this.postInvalidate();//刷新屏幕
}
}

Gallery 中的图片循环播放图片的实现

主要讲的就是一个循环播放图片的实现,我们这里主要还是用到了arraylisy,利用arraylist来控制里面的图片位置,这样就相对简单和容易控制循环了,我们只要牵扯到自定义的arrayadapter还是要注意继承getview在里面设置一个变量参数,就可以来实现变化,这才是最关键的。我们来看看代码吧:

 代码如下 复制代码
public class Splash extends Activity {
ArrayList objects = new ArrayList();
Gallery g;
int i = 0;
@Override
public void onCreate(Bundle icicle) { super.onCreate(icicle);
setContentView(R.layout.photos);
g = (Gallery) findViewById(R.id.gallery);
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
g.setAdapter(new CustomAdapter(this, objects));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
Log.i(“”, “selected ” + arg2);
}
@Override
public void onNothingSelected(AdapterView arg0) {}
});
}
@Override
public void onBackPressed() {
g.setSelection(i++);
}
private class CustomAdapter extends BaseAdapter {
private Context mCtx;
private List objects;
public int getCount() {
return this.objects.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public CustomAdapter(Context context, ArrayList objects) {
super();
mCtx = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView row = (ImageView) convertView;
if (row == null) {
row = new ImageView(mCtx);
row.setBackgroundDrawable(objects.get(position));
}
return row;
}
}
}

viewpager 循环播放图片 本站有很多种例子了,这里就不介绍了

本文章来给大家详细介绍Android开发时间日期格式国际化实现方法,有需要了解日期格式化的朋友可进入参考参考。

DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.

To format a date for the current Locale, use one of the static factory methods:

 代码如下 复制代码
 myString = DateFormat.getDateInstance().format(myDate); 

 

If you are formatting multiple dates, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.

 代码如下 复制代码

 DateFormat df = DateFormat.getDateInstance(); 
 for (int i = 0; i < a.length; ++i) { 
     output.println(df.format(myDate[i]) + "; "); 
 } 
 

To format a number for a different locale, specify it in the call to getDateInstance:

 代码如下 复制代码

 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); 
 


DateFormat can also be used to parse strings:

 代码如下 复制代码
 myDate = df.parse(myString); 

 
例子

 代码如下 复制代码

public static CharSequence formatTimeInListForOverSeaUser(
final Context context, final long time, final boolean simple,
Locale locale) {
final GregorianCalendar now = new GregorianCalendar();
// special time
if (time < MILLSECONDS_OF_HOUR) {
return "";
}
// today
final GregorianCalendar today = new GregorianCalendar(
now.get(GregorianCalendar.YEAR),
now.get(GregorianCalendar.MONTH),
now.get(GregorianCalendar.DAY_OF_MONTH));
final long in24h = time - today.getTimeInMillis();
if (in24h > 0 && in24h <= MILLSECONDS_OF_DAY) {
java.text.DateFormat df = java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale);
return "" + df.format(time);
}
// yesterday
final long in48h = time - today.getTimeInMillis() + MILLSECONDS_OF_DAY;
if (in48h > 0 && in48h <= MILLSECONDS_OF_DAY) {
return simple ? context.getString(R.string.fmt_pre_yesterday)
: context.getString(R.string.fmt_pre_yesterday)
+ " "
+ java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale).format(
time);
}
final GregorianCalendar target = new GregorianCalendar();
target.setTimeInMillis(time);
// same week
if (now.get(GregorianCalendar.YEAR) == target
.get(GregorianCalendar.YEAR)
&& now.get(GregorianCalendar.WEEK_OF_YEAR) == target
.get(GregorianCalendar.WEEK_OF_YEAR)) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("E", locale);
final String dow = "" + sdf.format(time);
return simple ? dow : dow
+ java.text.DateFormat.getTimeInstance(
java.text.DateFormat.SHORT, locale).format(time);
}
// same year
if (now.get(GregorianCalendar.YEAR) == target
.get(GregorianCalendar.YEAR)) {
return simple ? java.text.DateFormat.getDateInstance(
java.text.DateFormat.SHORT, locale).format(time)
: java.text.DateFormat.getDateTimeInstance(
java.text.DateFormat.SHORT,
java.text.DateFormat.SHORT, locale).format(time);
}
return simple ? java.text.DateFormat.getDateInstance(
java.text.DateFormat.SHORT, locale).format(time)
: java.text.DateFormat.getDateTimeInstance(
java.text.DateFormat.SHORT, java.text.DateFormat.SHORT,
locale).format(time);
}

标签:[!--infotagslink--]

您可能感兴趣的文章: