/**
* Created by qssq on 2018/7/31 qssq666@foxmail.com
*/
public class ActivityToast extends Activity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getAttributes().flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
getWindow().getAttributes().format = PixelFormat.TRANSLUCENT;
Intent intent = getIntent();
String title = intent.getStringExtra("title");
int duration = intent.getIntExtra("duration", 1000);
FrameLayout frameLayout = new FrameLayout(this);
TextView view = new TextView(this);
view.setText(title);
view.setPadding(30, 10, 10, 10);
frameLayout.addView(view);
setContentView(frameLayout);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, duration);
}
public static void showToast(Context context, String message, int duration) {
Intent intent = new Intent(context, ActivityToast.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", message);
intent.putExtra("duration", duration);
context.startActivity(intent);
}
}
缺点明显,影响返回键操作。影响任务栈,因此不可行。