我可以结合我的code到某种"全球经济活动"?我的、全球经济、code、QUOT

2023-09-12 10:08:38 作者:青婉

有没有在Android上的任何全球性活动,例如,我把我的code在这一个活动,它会影响所有的活动在我的项目?出现这种情况的我,因为同样的code被写入多种活动,如 KeyEvent.KEY code_BACK

Is there any global activity on Android such that I put my code in that one activity, and it affects all activities in my project? This occurs to me because the same code is written in multiple activities like KeyEvent.KEYCODE_BACK

例如这里我用:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            try {
                final Intent itnt_BackServices = new Intent(this,
                        BackServices.class);
                AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
                alertbox.setTitle("Touch signs");
                alertbox.setMessage("Do you want to quit!");
                alertbox.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                                stopService(itnt_BackServices);
                                mPlayer.stop();

                                finish();
                            }
                        });
                alertbox.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                            }
                        });
                alertbox.show();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return false;
    }

我复制并在每个活动的粘贴,我宁愿使用某种全球性的活动。

I copy and paste this in each activity, and I would rather use some kind of global activity.

推荐答案

您可以创建扩展活动 A类,然后扩展 CustomActivity 所有活动类是这样的。

You can create a class that extends Activity and then extend the CustomActivity to all the Activity Class like this.

public abstract class CustomActivity extends Activity{

    public abstract void initComponents();  // you can create a abstract method
    public abstract void addListner();       // you can create a abstract method

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

         if ((keyCode == KeyEvent.KEYCODE_BACK)) {
              // your stuff here....
            }
        return true;
    }
}

现在你可以延长延长任何类活动

Now you can extend this class where you want to extend any class with Activity.