2014-11-04 49 views
-2

如何在Android中使用后台服务获取数据?

我写这个服务,但它不工作 我想知道, 当我的应用程序安装在Android设备上我想从其他应用程序,数据,当其他应用程序使用剪贴板 所以我必须得到复制到剪贴板的数据(但在其他应用程序中复制)。 例如比如复制泡沫应用 请帮我如何写后台服务

+0

邮政编码无论你尝试。 – 2014-11-04 12:46:30

+0

看到这个https://developer.android.com/training/run-background-service/create-service.html – Rohit5k2 2014-11-04 12:47:12

+0

M尝试OnPrimaryClipChangedListener,但它不给我数据时,我的应用程序是关闭的移动任务管理器,所以我怎么能得到当我的应用程序被移动任务管理器关闭时的数据如何我可以写入后台服务 – 2014-11-05 12:29:40

回答

0

看看这个:

OnPrimaryClipChangedListener

我如何写一个服务,一旦是这样的:

public class BackgroundService extends Service { 

Notification notification; 
NotificationCompat.Builder builder; 
NotificationManager notificationManager; 


@Override 
public void onCreate() { 

} 

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

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    builder = new NotificationCompat.Builder(this); 
    Intent notificationIntent = new Intent(this, Main.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 


builder.setContentIntent(pendingIntent) 
      .setContentTitle("Title") 
      .setContentText("some text") 
      .setSmallIcon(R.drawable.someIcon) 
      .setWhen(System.currentTimeMillis()); 
    notification = builder.build(); 
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 


Log.e("service", "Started"); 
    startForeground(99, notification); 

//do your stuff here 

return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    } 
} 

然后使用意向启动它:

startService(new Intent(getBaseContext(), BackgroundService.class)); 

我没有试过这段代码,但我认为它应该可以工作;)

成功!

+0

M尝试OnPrimaryClipChangedListener但它当我的应用程序从移动任务管理器关闭时,不给我数据,所以当我的应用程序被移动任务管理器关闭时,如何获取数据我如何编写后台服务 – 2014-11-05 12:24:40

+0

@Anil我添加了一些代码,您可以如何创建后台服务:p让我知道它是否有效 – Rikkert09 2014-11-05 12:57:49

+0

不,它不工作 – 2014-11-09 12:57:33

相关问题