java - 安卓 : Some time service is stop in some application

标签 java android service

现在我在 Android 服务部门工作。我让文件夹观察员投入使用。所以当文件夹中的任何移动都会产生一些事件。

代码如下:

    @Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();                                               
    Log.v(TAG, "oncreate"); 
    Log.e(TAG, "onStart");

}   

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(TAG, "onStart");

    pathToWatch = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() +"/Screenshots/";                

            observer = new FileObserver(pathToWatch) {
                @Override                         
                public void onEvent(int event, final String file) {

                    if(event == FileObserver.CREATE)
                    {
                        String Path= pathToWatch+file;
                        Intent intent = new Intent();                                       
                        intent.setAction(MY_ACTION);                                                                    
                        intent.putExtra("DATAPASSED", Path);
                        sendBroadcast(intent);
                    }
                    else
                    {
                        //Log.d(TAG, "File Else [" + pathToWatch + file + "]");
                    }              
                }           
            };                     
            observer.startWatching();   


    return Service.START_STICKY;
    }

我的问题是:

  1. 第一次启动应用程序时它不工作,但第二次启动应用程序时它正常工作。

  2. 一段时间后我的服务自动停止所以我的要求是服务运行很长时间不自动停止。

有人知道我的问题吗?

提前谢谢你...

最佳答案

Android 操作系统可以根据剩余内存量来运行更重要的事情(系统进程、前台应用程序、可见服务)来自由终止任何服务,请参阅 http://developer.android.com/reference/android/app/Service.html :

Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implement onStartCommand() to schedule work to be done asynchronously or in another thread, then you may want to use START_FLAG_REDELIVERY to have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it.

...

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

所以解决方案很明确,要么

  1. 调用startForeground() , 或
  2. 使用 START_FLAG_REDELIVERY 将您的服务设计为稳健的

编辑: 下面是一个使用 startForeground() 的例子:

Intent i = new Intent(this, MainActivity.class);
startForeground(
        NOTIF_MONITORING,
        new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.ic_notification,
                        Notification.PRIORITY_DEFAULT)
                .setLargeIcon(
                        BitmapFactory.decodeResource(getResources(),
                                R.drawable.ic_launcher))
                .setOngoing(true)
                .setTicker(getString(R.string.notif_ticker))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(info)
                .setContentIntent(
                        PendingIntent.getActivity(this, 0, i, 0))
                .build());

请注意,这会导致在 Notification 中显示持久通知,这是从 4.​​0 左右开始需要的,以防止恶意应用程序在后台 secret 运行。如果您发现此要求不受欢迎,则必须采用 START_FLAG_REDELIVERY 路线。

关于java - 安卓 : Some time service is stop in some application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22406194/

相关文章:

Android 服务将一个 Activity 的多个实例放入历史记录中

windows - 创建一个服务作为自动运行

java - Tess4J,找不到 tessdata

java - java中的mongodb嵌套搜索查询

java - 错误 :(27, 0) android studio 错误

android - 从服务访问传感器

service - 本地计算机上的 Datastax cassandra 社区服务器 2.1.10 服务启动然后停止

java - Map<key, List/Set<key定义类型的对象>>

android - 如何在 Intent 服务中获取上下文

java - 从android中的assets文件中读取