我正在开发一款壁纸应用。如果该文件夹不存在,则在外部存储中创建文件夹。用户设置的时间间隔。然后,应用程序将在用户提供的时间间隔内从该文件夹设置壁纸(如果不为空)。
File dir= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"imagesWallpaper");
if(!dir.isDirectory())
dir.mkdirs();
不是创建文件夹i外部记忆,不知道为什么
2.I触发了壁纸服务,但我不知道为什么它没有启动WallpaperEngine类。请帮忙。
WallpaperMainActivity:
public class WallpaperMainActivity extends Activity {
private RadioGroup radioGroup;
public long interval = 0;
File[] wallpaperImages = null;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wall_paper_manager);
File dir= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"imagesWallpaper");
if(!dir.isDirectory())
dir.mkdirs();
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
long time =Integer.valueOf((String) findViewById(checkedId).getTag());
interval = time*60000;
scheduleAlarm();
}
});
}
public void scheduleAlarm()
{
Intent intent = new Intent(WallpaperMainActivity.this, WallpaperSrvc.class);
PendingIntent pintent = PendingIntent.getService(WallpaperMainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC, 10*1000, interval, pintent);
}
}
WallpaperSrvc和WallpaperEngine课程:
public class WallpaperSrvc extends WallpaperService {
File[] wallpaperImages = null;
int count = 0;
Bitmap tmp = null;
//Drawable drawable;
@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
public class WallpaperEngine extends Engine {
private boolean mVisible = true;
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible)
{
getImage();
draw();
}
else
{
stopSelf();// stop the wallpaper
}
super.onVisibilityChanged(visible);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep, int xPixelOffset,
int yPixelOffset) {
draw();
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
xPixelOffset, yPixelOffset);
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
getImage();
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
stopSelf();
// stop the wallaperservice
}
}
public void getImage()
{
File dir= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"imagesWallpaper");
if(!dir.exists())
dir.mkdir();
else
wallpaperImages = dir. listFiles();
if(count < wallpaperImages.length)
{
this.tmp = BitmapFactory.decodeFile(wallpaperImages[count].getAbsolutePath());
count++;
}
else
{
count = 0;
this.tmp = BitmapFactory.decodeFile(wallpaperImages[count].getAbsolutePath());
}
};
public void draw() {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try
{
//Bitmap wallpaper = ((BitmapDrawable) drawable).getBitmap();
myWallpaperManager.setBitmap(tmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}
清单:
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name=".WallpaperSrvc"
android:enabled="true"
android:label="wallpaper_service"
android:icon="@drawable/ic_launcher"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" >
</action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/mywallpaper" >
</meta-data>
</service>
<activity
android:name=".WallpaperMainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.WallpaperSettings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".WallpaperSrvc"/>
</application>
</manifest>
主要活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:text="@string/Discription"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/one_day"
android:clickable="true"
android:orientation="vertical"
android:scrollbars="vertical" >
<RadioButton
android:id="@+id/oneminute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 minute"
android:tag="1"/>
<RadioButton
android:id="@+id/thirty_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="30 minutes"
android:tag="30"/>
<RadioButton
android:id="@+id/one_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 hour"
android:tag="60"/>
<RadioButton
android:id="@+id/two_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 hours"
android:tag="120"/>
<RadioButton
android:id="@+id/six_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 hours"
android:tag="360"/>
<RadioButton
android:id="@+id/tvlv_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12 hours"
android:tag="720"/>
<RadioButton
android:id="@+id/one_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 day"
android:tag="1440"/>
<RadioButton
android:id="@+id/three_days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 days"
android:tag="4320"/>
<RadioButton
android:id="@+id/oneweek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="week"
android:tag="10080"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
RES / XML / mywallpaper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/ic_launcher"
android:description="@string/wallpaper_description"
android:settingsActivity="com.wallpaper.WallpaperMainActivity"/>
答案 0 :(得分:0)
您必须使用写入外部存储权限。这就是为什么它没有创建你的目录。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单中添加此警报和壁纸持久性