如何让应用程序复制文本并自动发送到我的联网数据库?

Posted

技术标签:

【中文标题】如何让应用程序复制文本并自动发送到我的联网数据库?【英文标题】:How to make an app to copy text and send automatically to my networked database? 【发布时间】:2018-11-29 11:29:01 【问题描述】:

我正在尝试使用剪贴板管理器制作 android 应用

使用此代码-

final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.addPrimaryClipChangedListener( new ClipboardManager.OnPrimaryClipChangedListener() 
    public void onPrimaryClipChanged() 
        String a = clipboard.getText().toString();
        Toast.makeText(getBaseContext(),"Copy:\n"+a,Toast.LENGTH_LONG).show();
    
);

我的应用旨在复制文本并自动发送我的服务器数据库。关于创建这个有什么想法吗?

【问题讨论】:

【参考方案1】:

首先创建一个服务,即使应用程序关闭也会监听

public class CBWatcherService extends Service 

private final String tag = "[[ClipboardWatcherService]] ";  
private OnPrimaryClipChangedListener listener = new OnPrimaryClipChangedListener() 
    public void onPrimaryClipChanged() 
        performClipboardCheck();
    
;

@Override 
public void onCreate() 
    ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).addPrimaryClipChangedListener(listener);


@Override
public int onStartCommand(Intent intent, int flags, int startId) 
    File folder = new File(ClipboardCacheFolderPath);
    // ClipboardCacheFolderPath is a predefined constant with the path
    // where the clipboard contents will be written

    if (!folder.exists())  folder.mkdir(); 
    return START_STICKY;


@Override
public IBinder onBind(Intent intent) 
    return null;


private void performClipboardCheck() 
    ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    if (cb.hasPrimaryClip()) 
        ClipData cd = cb.getPrimaryClip();
        if (cd.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) 
            try 
                File folder = new File(ClipboardCacheFolderPath);
                if (!folder.exists())  folder.mkdir(); 
                Calendar cal = Calendar.getInstance();
                String newCachedClip = 
                    cal.get(Calendar.YEAR) + "-" +
                    cal.get(Calendar.MONTH) + "-" +
                    cal.get(Calendar.DAY_OF_MONTH) + "-" +
                    cal.get(Calendar.HOUR_OF_DAY) + "-" +
                    cal.get(Calendar.MINUTE) + "-" +
                    cal.get(Calendar.SECOND);

                // The name of the file acts as the timestamp (ingenious, uh?)
                File file = new File(ClipboardCacheFolderPath + newCachedClip);
                file.createNewFile();
                BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));
                bWriter.write((cd.getItemAt(0).getText()).toString());
                bWriter.close();

// Here send the file or the text you want to send to the server or firebase
//callApi()
            
            catch (IOException e) 
                e.printStackTrace();
              
        
    


【讨论】:

以上是关于如何让应用程序复制文本并自动发送到我的联网数据库?的主要内容,如果未能解决你的问题,请参考以下文章

如何启用 iOS 文件应用程序以将文件复制到我的应用程序中?

用户发送短信时如何关闭短信应用程序并返回活动? [复制]

如何正确使用emailintent? [复制]

将文本文件合并到我的应用程序的构建中

Android:如何在手机上网时自动上传数据/同步到服务器

如何将数据从服务器发送到Android?