所以我希望在收到短信时在屏幕上显示一个对话框,并在5秒后将其从屏幕上移除,但也可以点击它并取消定时器以将其保留在屏幕上。所以我知道我有几个选择。我可以使用Handler,Timer或AlarmManager(其他任何一个?)在我的情况下最好的是什么?你能举例说明如何使用最佳选择吗?
答案 0 :(得分:1)
首先为您的视图创建布局,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/black">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="4"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/exit_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/white"
android:text="Exit" />
<Button
android:id="@+id/stay_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/white"
android:text="Stay" />
</LinearLayout>
现在创建严格来构建您的视图:
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
public class BubbleViewService extends Service
{
private WindowManager windowManager = null;
private View rootView = null;
private Intent intent = null;
private int timeToExit = 5000;
private boolean autoExitFlag = true;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
this.intent = intent;
Log.v("BubbleViewService", "onStartCommand");
iniView();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate()
{
super.onCreate();
Log.v("BubbleViewService", "onCreate");
}
private void iniView()
{
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
rootView = View.inflate(this, R.layout.my_dialog, null);
final WindowManager.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP|Gravity.LEFT;
params.x = 0;
params.y = 100;
TextView tvMessage = (TextView) rootView.findViewById(R.id.textView1);
if(intent!=null)
{
String message = intent.getStringExtra("Msg");
tvMessage.setText(message);
}
windowManager.addView(rootView, params);
startExitHanlder();
rootView.findViewById(R.id.exit_button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
BubbleViewService.this.stopSelf();
}
});
rootView.findViewById(R.id.stay_button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
autoExitFlag = false;
}
});
rootView.setOnTouchListener(new OnTouchListener()
{
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int)(event.getRawX() - initialTouchX);
params.y = initialY + (int)(event.getRawY() - initialTouchY);
windowManager.updateViewLayout(rootView, params);
return true;
}
return false;
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
if(rootView!=null) windowManager.removeView(rootView);
}
private void startExitHanlder()
{
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if(autoExitFlag==true) BubbleViewService.this.stopSelf();
}
}, timeToExit);
}
}
您需要将此服务添加到清单xml文件中:
<service
android:name="your.pkg.path.BubbleViewService"
android:label="My Service" >
最后,根据您的应用程序活动,根据需要使用以下代码启动此服务:
Intent intent = new Intent(MainActivity.this,BubbleViewService.class);
intent.putExtra("Msg", "Blah blah blah ... what ever");
MainActivity.this.startService(intent);
正如我所说,您可以根据应用程序要求自定义服务,在此示例中,如果auto exit标志为true,我会在5秒后添加处理程序以停止服务。
答案 1 :(得分:0)
建议的解决方案非常有趣,但是当对话框位于顶部时,无法单击背景上的任何图标或元素。如何实现这一目标?