壁紙アプリを開発しています。そのフォルダーが存在しない場合、外部ストレージにフォルダーを作成します。ユーザー設定の時間間隔。次に、アプリは、ユーザーが指定した間隔でそのフォルダーから壁紙 (空でない場合) を設定します。
File dir= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"imagesWallpaper");
if(!dir.isDirectory()) dir.mkdirs();
フォルダを作成していません。外部メモリがありません。理由はわかりません
2.壁紙サービスをトリガーしましたが、なぜ 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"/>