我有一个显示警告图像的Android原生应用程序,应用程序在后台工作,我想在屏幕上显示图像5秒,无论用户在哪个屏幕上。 它甚至可能吗?
应该是这样的: click here to see image
答案 0 :(得分:-1)
public class FloatingDialogActivity extends Activity implements View.OnTouchListener {
private static final String TAG = "FloatingDialogActivity";
RelativeLayout topLay;
FrameLayout draggableView;
float dX;
float dY;
int lastAction;
private boolean shouldClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.floating_dialog_layout);
topLay=(RelativeLayout)findViewById(R.id.topLay);
draggableView=(FrameLayout)findViewById(R.id.draggableView);
draggableView.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
shouldClick = true;
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
shouldClick = false;
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (shouldClick)
view.performClick();
if (lastAction == MotionEvent.ACTION_DOWN)
// Toast.makeText(IncomingCallActivity.this, "Clicked!", Toast.LENGTH_SHORT).show();
break;
default:
return false;
}
return true;
}
}
使用以下布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parentLay"
>
<FrameLayout
android:layout_width="match_parent"
android:id="@+id/draggableView"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/topLay"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:background="#4954a3"
android:gravity="center">
<TextView
android:id="@+id/customerNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:text="New User"
android:textColor="#FFFFFF"
android:textSize="20dp"
/>
</RelativeLayout>
</FrameLayout>
在清单文件中添加活动主题
<activity
android:name="vivekdemo.FloatingDialogActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.Transparent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
在styles.xml中添加以下主题
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>