我正在尝试制作一个将相机的实时反馈设置为壁纸的Android应用程序。我差不多完成了编码部分。事情就是当我点击设置壁纸按钮时,动态壁纸选择器菜单打开。但是当我在其中选择我的应用程序时,应用程序崩溃了。我知道它有点现有的应用程序,但它是客户的需求。
WelcomeActivity.java
package com.wallpaper.transparenthighdefcamera;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class WelcomeActivity extends Activity {
ImageButton imgButt;
public WelcomeActivity()
{
}
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.activity_main);
imgButt=(ImageButton) findViewById(R.id.setButton);
imgButt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= 16)
{
intent.setAction("android.service.wallpaper.CHANGE_LIVE_WALLPAPER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.wallpaper.transparenthighdefcamera", "com.wallpaper.transparenthighdefcamera.TransparentWallpaperService"));
} else
intent.setAction("android.service.wallpaper.LIVE_WALLPAPER_CHOOSER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.wallpaper.transparenthighdefcamera", "com.wallpaper.transparenthighdefcamera.TransparentWallpaperService"));
//}
startActivity(intent);
}
});
}
}
TransparentWallpaperService.java:
package com.wallpaper.transparenthighdefcamera;
import com.wallpaper.transparenthighdefcamera.GenericaCamera;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
public class TransparentWallpaperService extends WallpaperService {
private class MyWallpaperEngine extends android.service.wallpaper.WallpaperService.Engine
{
GenericaCamera GC;
final TransparentWallpaperService this$0;
public void onCreate(SurfaceHolder surfaceholder)
{
if (GC == null)
{
try
{
if (TransparentWallpaperService.existing != null)
{
TransparentWallpaperService.existing.destroyExisting();
}
}
catch (Exception exception) { }
GC = new GenericaCamera(surfaceholder, getBaseContext());
TransparentWallpaperService.existing = GC;
}
super.onCreate(surfaceholder);
}
private MyWallpaperEngine()
{
super();
this$0 = TransparentWallpaperService.this;
}
MyWallpaperEngine(MyWallpaperEngine mywallpaperengine)
{
this();
}
}
public static GenericaCamera existing;
public TransparentWallpaperService()
{
}
public void onCreate()
{
super.onCreate();
}
public android.service.wallpaper.WallpaperService.Engine onCreateEngine()
{
return new MyWallpaperEngine(null);
}
public void onDestroy()
{
super.onDestroy();
}
}
GenricaCamera.java:
package com.wallpaper.transparenthighdefcamera;
import java.util.Iterator;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.widget.Toast;
public class GenericaCamera implements android.view.SurfaceHolder.Callback {
private static boolean isPreviewRunning = false;
private Camera cameraDevice;
private SurfaceHolder cameraSurfaceHolder;
private Context context;
public GenericaCamera(SurfaceHolder surfaceholder, Context context1)
{
cameraDevice = null;
cameraSurfaceHolder = null;
context = context1;
cameraSurfaceHolder = surfaceholder;
cameraSurfaceHolder.setType(3);
cameraSurfaceHolder.addCallback(this);
}
private static android.hardware.Camera.Size getBestPreviewSize(int i, int j, android.hardware.Camera.Parameters parameters)
{
android.hardware.Camera.Size size = null;
Iterator iterator = parameters.getSupportedPreviewSizes().iterator();
do
{
android.hardware.Camera.Size size1;
do
{
if (!iterator.hasNext())
{
return size;
}
size1 = (android.hardware.Camera.Size)iterator.next();
} while (size1.width > i || size1.height > j);
if (size == null)
{
size = size1;
} else
{
int k = size.width * size.height;
if (size1.width * size1.height > k)
{
size = size1;
}
}
} while (true);
}
public void destroyExisting()
{
if (cameraDevice != null)
{
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
}
isPreviewRunning = false;
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k)
{
if (cameraDevice != null)
{
if (isPreviewRunning)
{
cameraDevice.stopPreview();
}
android.hardware.Camera.Parameters parameters = cameraDevice.getParameters();
android.hardware.Camera.Size size = getBestPreviewSize(j, k, parameters);
if (size != null)
{
parameters.setPreviewSize(size.width, size.height);
}
cameraDevice.setParameters(parameters);
cameraDevice.startPreview();
isPreviewRunning = true;
}
}
public void surfaceCreated(SurfaceHolder surfaceholder)
{
try
{
if (cameraDevice == null)
{
cameraDevice = Camera.open();
cameraDevice.setDisplayOrientation(90);
cameraDevice.setPreviewDisplay(cameraSurfaceHolder);
}
cameraDevice.startPreview();
return;
}
catch (Exception exception)
{
Toast.makeText(context, "Can't create preview!", 1).show();
exception.printStackTrace();
return;
}
}
public void surfaceDestroyed(SurfaceHolder surfaceholder)
{
if (cameraDevice == null)
{
return;
} else
{
destroyExisting();
return;
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wallpaper.transparenthighdefcamera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.software.live_wallpaper" android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.wallpaper.transparenthighdefcamera.WelcomeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".services.TransparentCameraHelperService" />
<service android:name=".services.TransparentWallpaperService" android:permission="android.permission.BIND_WALLPAPER" android:enabled="true">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" />
</service>
</application>
</manifest>