我正在尝试使用一个浮动小部件,该小部件在调用onPause时会弹出并在单击时重新启动该活动,因此我使用了FLAG_ACTIVITY_SINGLE_TOP,因此它不会启动新的活动,但是如果显示以下内容,则会花费很多时间它已经打开了。
package com.example.myapplication;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
askForSystemOverlayPermission();
}
private void askForSystemOverlayPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
Log.d("mmm", "asking permission");
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1);
}
}
@Override
protected void onPostResume() {
super.onPostResume();
stopService(new Intent(this, MyService.class));
}
@Override
protected void onPause() {
super.onPause();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Settings.canDrawOverlays(this) || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
startService(new Intent(this, MyService.class));
}
}
}
package com.example.myapplication;
public class MyService extends Service {
private WindowManager manager;
private ImageView v;
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate() {
super.onCreate();
v = new ImageView(this);
v.setImageResource(R.drawable.ic_launcher_background);
manager = (WindowManager) getSystemService(WINDOW_SERVICE);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GoToActivity();
}
});
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
150, 150,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
manager.addView(v, params);
v.setOnTouchListener(new View.OnTouchListener() {
float dX;
float dY;
int lastAction;
@Override
public boolean onTouch(View v, MotionEvent event) {
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) v.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = layoutParams.x - event.getRawX();
dY = layoutParams.y - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
layoutParams.y = (int) (event.getRawY() + dY);
layoutParams.x = (int) (event.getRawX() + dX);
manager.updateViewLayout(v, layoutParams);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
break;
default:
return false;
}
return false;
}
});
}
private void GoToActivity() {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
stopSelf();
startActivity(i);
}
@Override
public void onDestroy() {
super.onDestroy();
manager.removeView(v);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
答案 0 :(得分:0)
为什么不将MainActivity
与FLAG_ACTIVITY_SINGLE_TOP
一起启动,为什么不直接将现有任务推进呢?
代替此:
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
stopSelf();
startActivity(i);
执行此操作:
Intent i = getPackageManager().getLaunchIntentForPackage("may.package.name");
stopSelf();
startActivity(i);