我正在研究一种应用程序,允许用户在将选择设置为当前正在运行的动态壁纸之前在多个动画之间进行选择。
我查看了具有类似问题的链接,例如: call Live wallpaper within application
并且: Setting live wallpaper programmatically
他们解决了如何进行选择的问题,而不是我看到的错误。
支付给用户的第一个屏幕是显示多个墙纸的选择屏幕。每个墙纸都有一个供用户单击的按钮,用于指示该墙纸的选择。
每个按钮都有一个唯一的ID。找到每个对象并将其分配给Button对象的实例,并向OnClickListener注册。
单击按钮后,我将使用switch语句确定按下了哪个按钮,创建了一个新的Intent,指示要运行的墙纸类,然后开始活动。
当我在模拟器中运行它时,应用程序将按预期加载。我看到我的选择屏幕,它正确显示了各种墙纸选择。选择墙纸运行时会发生问题。单击按钮后,显示屏将变成白色,并一直保持这种状态,直到我关闭调试器。
在调试模式下运行此代码后,我发现单击选择按钮后,代码将执行switch语句,并通过创建调用startActivity(intent)方法的intent进行创建,然后中断。然后,代码会跳过View.java,Handler.java和Looper.java类,并最终在Looper.java类中的for循环中永久循环,该循环处理消息队列。
这是MainActivity,在其中设置初始布局视图,设置按钮并创建onClickListener:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection);
// Capture our buttons from the selection layout
Button button_1 = (Button)findViewById(R.id.set1);
button_1.setOnClickListener(this);
// Capture our buttons from the selection layout
Button button_2 = (Button)findViewById(R.id.set2);
button_2.setOnClickListener(this);
// Capture our buttons from the selection layout
Button button_3 = (Button)findViewById(R.id.set3);
button_3.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
switch(v.getId())
{
//If button_1 was selected, set the first wallpaper.
case R.id.set1 :
setContentView(R.layout.activity_main);
Intent intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(this, Wallpaper1.class));
startActivity(intent);
break;
//If button_2 was selected, set the second wallpaper.
case R.id.set2 :
setContentView(R.layout.activity_main);
intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(this, Wallpaper2.class));
startActivity(intent);
break;
//If the Cuba button was selected, set the Cuba wallpaper.
case R.id.set3 :
setContentView(R.layout.activity_main);
intent = new Intent(
WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
new ComponentName(this, Wallpaper3.class));
startActivity(intent);
break;
}
}
这是消息循环,执行被卡住,无限循环:
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
final long traceTag = me.mTraceTag;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
try {
msg.target.dispatchMessage(msg);
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
在正确移动switch语句之后,代码似乎没有注意到click事件。
我该如何正确调用Intent?