通过单击chathead(小部件)保存来自Web /其他应用程序的文本选择

时间:2014-08-21 19:43:08

标签: android

我想要做的是类似于Diigo"复制泡泡" (类似聊天的Facebook。从现在开始称为Chathead),它基本上允许用户在外部app / webb浏览器中选择文本,并将所选文本保存到App"记事本中。点击Chathead的活动:

http://stadt-bremerhaven.de/copy-bubble-clipboard-manager-fuer-android/

流程: 1.通过“共享”功能(从其他应用程序)启动窗口小部件

  1. 小部件显示1个按钮。单击该按钮可启动Chathead并关闭第一个小部件。

  2. 用户现在可以选择文本字符串,然后单击Chathead。这将做两件事:

    a)更改Chathead的文本。例如,如果用户选择"红球" Chathead现在将有一个可见的标题说"红球"。

    b)将文本字符串保存到我的应用程序的另一个活动中(在Diigo中,它将是Diio应用程序中的记事本)。我尚未创建此活动,因此下面没有示例代码。

  3. 当"扔掉"聊天头(就像Facebook聊天头一样)。聊天对象已被销毁" /已关闭。

  4. 到目前为止我的内容如下:

    ANDROIDMANIFEST.XML (将我的应用添加到用户可以与其他应用分享内容的应用列表中。此时,分享到我的应用将打开正常的活动,而不是小部件。此活动显示单击按钮,将启动Chathead。要启动Chathead,我还在清单中定义了一个服务)

    <activity android:name=".NewOrExistingObjectPicker"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    
       <service
            android:name="kowkeekowks.trackabo.Flyer"
            android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="text/plain" />
        </intent-filter>
       </service>
    

    NewOrExistingObjectPicker.java (显示按钮的活动。点击按钮将打开Chathead。 问题: 我排除了部分内容我在网上找到的代码,因为它给了我错误,我还没弄清楚它实际上做了什么。如果有人能解释这些行的功能,那将是感激不尽的。)

    public class NewOrExistingObjectPicker extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_shared_content);
    
        //Intent intent = getIntent();
        //String action = intent.getAction();
    
      //  if (Intent.ACTION_SEND.equals(action) && type != null) {
       //     Log.d("OMG74", action);
       //     startService(new Intent(NewOrExistingObjectPicker.this, Flyer.class));
      //  }
    
    Button launch = (Button)findViewById(R.id.button1);
        launch.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
               // Log.d("OMG75", type1);
                startService(new Intent(NewOrExistingObjectPicker.this, Flyer.class));
            }
        });
    
        Button stop = (Button)findViewById(R.id.button2);
        stop.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                stopService(new Intent(NewOrExistingObjectPicker.this, Flyer.class));
            }
        });
    
    }
    
    //  @Override
    //  protected void onResume() {
        //Intent intent = getIntent();
        //String action = intent.getAction();
    
    //    if (Intent.ACTION_SEND.equals(action) && type != null)
     //   {
      //      startService(new Intent(NewOrExistingObjectPicker.this, Flyer.class));
      //  }
      //  super.onResume();
    // }
    }
    

    Flyer.java (这是我的Chathead。 问题 :是否可以使用用户可以移动的按钮以及单击 - 即响应onTouch()和onClick()?现在我将onTouch()方法附加到WindowManager本身,我需要在按钮的背景中放大图像,以便将Chathead拖动到不同的位置。 )

    public class Flyer extends Service {
    private ImageView chatHead;
    private RelativeLayout parentlayout;
    private WindowManager windowManager;
    
    private RelativeLayout border;
    int imageid = 1;
    int nameid = 3;
    Button button;
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
    
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    
        chatHead = new ImageView(this);
    
        chatHead.setImageResource(R.drawable.floating);
    
        chatHead.setId(imageid);
    
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
    
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;
    
        parentlayout = new RelativeLayout(this);
        button = new Button(this);
        button.setText("Address");
    
        final RelativeLayout.LayoutParams params_imageview = new 
    RelativeLayout.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
    
        params_imageview.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    
        final RelativeLayout.LayoutParams params_name = new RelativeLayout.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
    
        params_name.addRule(RelativeLayout.ALIGN_RIGHT, imageid);
        params_name.addRule(RelativeLayout.ALIGN_TOP, imageid);
    
    
        final RelativeLayout.LayoutParams params_button = new RelativeLayout.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT
        );
        params_button.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    
        parentlayout.addView(chatHead, params_imageview);
        parentlayout.addView(button, params_button);
    
    
        windowManager.addView(parentlayout, params);
        windowManager.updateViewLayout(parentlayout, params);
    
        parentlayout.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO: Set the selected string as button text. Save the selected string to another Activity in my app. 
    
            }
        });
    
    
        try {
            parentlayout.setOnTouchListener(new View.OnTouchListener() {
                private WindowManager.LayoutParams paramsF = params;
                private int initialX;
                private int initialY;
                private float initialTouchX;
                private float initialTouchY;
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            initialX = paramsF.x;
                            initialY = paramsF.y;
                            initialTouchX = event.getRawX();
                            initialTouchY = event.getRawY();
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                            paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                            windowManager.updateViewLayout(parentlayout, paramsF);
                            break;
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (parentlayout!= null) windowManager.removeView(parentlayout);
    }
    

    非常感谢我能得到的所有帮助! 苏珊娜

0 个答案:

没有答案