android - 在 Android 中恢复 Activity 状态

标签 android android-activity android-service

我想编写一个 Android 应用程序,通过在文本文件中输入照片 uri、时间戳和地理位置戳记来记录所有拍摄的照片。单击照片时应该会发生这种情况。
为此,我正在运行一个在默认照片目录上使用 FileObserver 的服务。 (我知道这不是万无一失的)。

用户最初会看到一个 GUI,他可以选择一个文件名和一个开始按钮来开始录制。当用户按下开始录制时,后台服务启动,用户返回拍摄一些照片,1 当他回来时,他应该可以选择停止录制,从而结束后台服务。

现在我的问题是,
1. Activity如何知道服务什么时候运行,什么时候不运行?
2. Activity 的先前状态如何恢复并重新连接到特定服务?当我恢复 Activity 时, Activity 与服务的关联如何发生? (如果我的 Activity 必须停止服务,则需要考虑某种关联)

这里是我的代码供引用:[ExperienceLoggerService是MainActivity的一个内部类]

public class ExperienceLoggerService extends Service
/* This is an inner class of our main activity, as an inner class makes good use of resources of outer class */
{


    private final IBinder mBinder = new LocalBinder();      
    File file;
    FileOutputStream fOut;
    OutputStreamWriter fWrite;
    /** Called when the activity is first created. */
    private void startLoggerService() 
    {
        try
        {
            //initialise the file in which to log
            this.file = new File(Environment.getExternalStorageDirectory(), "MyAPPNostalgia");
            System.out.println("1:"+Environment.getExternalStorageDirectory()+ "MyAPPNostalgia");
            file.createNewFile();
            fOut = new FileOutputStream(file);
            fWrite = new OutputStreamWriter(fOut);
        }
        catch(Exception e)
        {
            System.out.println("Error in logging data, blame navjot");
        }
        FileObserver observer = new MyFileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA");
        observer.startWatching(); // start the observer
    }

    @Override
    public void onCreate() 
    {
        super.onCreate();
        //mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        startLoggerService();

        // Display a notification about us starting. We put an icon in the
        // status bar.
        //showNotification();
    }

    public void onDestroy() 
    {
        try
        {
            //close the file, file o/p stream and out writer.
            fWrite.close(); 
            fOut.close();
        }

        catch(Exception e)
        {
        }

        super.onDestroy();

    }

    class MyFileObserver extends FileObserver
    {
        public MyFileObserver(String path) 
        {
            super(path);
        }

        public void onEvent(int event, String file) 
        {
            if(event == FileObserver.CREATE && !file.equals(".probe"))
            { // check if its a "create" and not equal to .probe because thats created every time camera is launched
                String fileSaved = "New photo Saved: " + file +"\n";
                try
                {
                    ExperienceLoggerService.this.fWrite.append(fileSaved);
                }
                catch(Exception e)
                {
                    System.out.println("Problem in writing to file");
                }
            }
        }
    }


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

    public class LocalBinder extends Binder 
    {
        ExperienceLoggerService getService() 
        {
            return ExperienceLoggerService.this;
        }
    }

}

最佳答案

您不需要与服务“连接”即可停止 Activity 。你可以这样做:

Intent intent = new Intent(this, ExperienceLoggerService.class);
stopService(intent);

我不确定您是否真的需要知道您的服务是否正在运行。如果您真的需要这样做,可以使用 ActivityManager.getRunningServices() 来完成,请参阅 http://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices%28int%29

编辑:关于绑定(bind)服务的注意事项

您没有发布 Activity 绑定(bind)到服务的代码,但再次查看您的源代码后,我发现您使用的是绑定(bind)服务。在这种情况下,您的 Activity 只需调用 unbindService() 即可,将调用 bindService() 时使用的相同 ServiceConnection 对象传递给它。一旦服务没有绑定(bind)的客户端,它就会自行关闭。

关于android - 在 Android 中恢复 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17636626/

相关文章:

android - RecyclerView取消选择之前选择的复选框

android - onStop() 中的 finish() 之后的 onCreate()

android - 当应用程序在后台运行时接收Intent(服务应用程序)

java - 从Service类中的BaseAdapter获取数据

android - 现在打牌

android - 来自不同表的联合列值

java - 3 旋转器不工作

android - 在 LibGDX 中生成一个随机数

android - 何时使用 Activity 转换与动态 fragment 的模式

java - 检查 Activity 是否是启动后的第一个 Activity