写一个android获取系统属性的例子
时间: 2023-03-19 12:20:52 浏览: 82
答:下面是一个Android程序,用来获取系统属性的例子:public class SystemProperties {public static String get(String key) {
String value = null;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
value = (String) get.invoke(c, key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}}
相关问题
写一个videoview 的例子
好的,以下是一个简单的VideoView例子:
首先,我们需要在布局文件中添加一个VideoView组件:
```
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
接着,我们在Activity中获取这个VideoView组件,并为其设置视频资源:
```
VideoView videoView = findViewById(R.id.video_view);
videoView.setVideoPath("path/to/your/video.mp4");
videoView.start();
```
这样,当我们启动这个Activity时,VideoView就会自动播放我们设置的视频资源。当然,我们也可以在代码中为VideoView设置一些其他的属性,例如:
```
// 设置是否循环播放
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
videoView.start();
}
});
// 设置是否全屏播放
videoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (videoView.isPlaying()) {
videoView.pause();
} else {
videoView.start();
}
}
return true;
}
});
```
这些属性可以让我们更加灵活地控制VideoView的播放行为。
请给出一个例子
好的,下面是一个使用AnimationDrawable动画实现启动图片加动画效果的例子:
1. 在res/drawable目录下创建一个XML文件,例如animation.xml,代码如下:
```
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/frame1" android:duration="200" />
<item android:drawable="@drawable/frame2" android:duration="200" />
<item android:drawable="@drawable/frame3" android:duration="200" />
</animation-list>
```
其中,每个item节点代表一帧动画,drawable属性指定该帧所使用的图片资源,duration属性指定该帧的持续时间。
2. 在布局文件中添加一个ImageView,例如:
```
<ImageView
android:id="@+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/animation" />
```
其中,src属性指定ImageView要显示的动画资源,即刚才创建的animation.xml文件。
3. 在Activity中启动动画,例如:
```
public class SplashActivity extends AppCompatActivity {
private ImageView mSplashImage;
private AnimationDrawable mAnimationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mSplashImage = findViewById(R.id.splash_image);
mAnimationDrawable = (AnimationDrawable) mSplashImage.getDrawable();
mAnimationDrawable.start();
// 等待动画结束后跳转到应用主界面
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
```
其中,mSplashImage是刚才在布局文件中添加的ImageView,mAnimationDrawable是动画对象,通过getDrawable()方法获取。在onCreate()方法中调用mAnimationDrawable.start()启动动画,同时通过Handler延时2秒后跳转到应用主界面。
希望这个例子对您有所帮助。