Android Magnet:桌面删除APP自动弹出垃圾桶接受图标删除动作
在Android系统上,当用户长按桌面的图标后,会自动从底部或者顶部弹出一个垃圾或者叉子的删除图标,用户拖曳欲删除的图标至垃圾箱后,则在桌面删除该APP。
在github上一个第三方开源的项目:Magnet实现的就是上述类似功能。如图(图片来源自Magnet项目包)所示:
Magnet在github上的项目主页是:https://github.com/premnirmal/Magnet
Magnet实现相对简单,开发文档和代码例子比较易懂。Magnet给出的demo代码例子是由一个主Activity启动一个Service,主Activity启动后就finsh自己,然后由Service接管用户的拖曳删除动作。
启动Service的MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class ParanormalActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this,MyService.class));
finish();
}
}
关键的代码章节(Service):
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.premnirmal.Magnet.IconCallback;
import com.premnirmal.Magnet.Magnet;
public class MyService extends Service implements IconCallback {
private static final String TAG = "Magnet";
private Magnet mMagnet;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
ImageView iconView = new ImageView(this);
iconView.setImageResource(R.drawable.ic_launcher);
mMagnet = new Magnet.Builder(this)
.setIconView(iconView)
.setIconCallback(this)
.setRemoveIconResId(R.drawable.trash)
.setRemoveIconShadow(R.drawable.bottom_shadow)
.setShouldFlingAway(true)
.setShouldStickToWall(true)
.setRemoveIconShouldBeResponsive(true)
.build();
mMagnet.show();
}
@Override
public void onFlingAway() {
Log.i(TAG, "onFlingAway");
}
@Override
public void onMove(float x, float y) {
Log.i(TAG, "onMove(" + x + "," + y + ")");
}
@Override
public void onIconClick(View icon, float iconXPose, float iconYPose) {
Log.i(TAG, "onIconClick(..)");
mMagnet.destroy();
}
@Override
public void onIconDestroyed() {
Log.i(TAG, "onIconDestroyed()");
}
}
相关权限需要添加:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>