0

初心者っぽい質問のために私を銛にしないでください。

SL4Aを使用してAndroidアプリケーションで作業しています。アプリケーションが起動すると、スクリプトの実行中にバックグラウンドで実行されます。どこから始めればよいかわかりませんが、アイコンをクリックするたびに、アプリケーションが再起動します。何も変わらずに、さまざまな起動モードを使用してみました。OnCreateコードと通知の設定に関係していると思います。アプリケーションの状態を保存してから、アイコンを再度クリックするか、通知バーからクリックして再開するのにサポートが必要です。私は助けを求めてここに向かわなければならなかったすべてを試しました。私は決してアンドロイドプログラミングのプロではありません。みんなありがとう、優しくしてください;)

         Public void onCreate() {
    super.onCreate();
    mInterpreterConfiguration = ((BaseApplication) getApplication())
            .getInterpreterConfiguration();
}

@Override
public void onStart(Intent intent, final int startId) {
    super.onStart(intent, startId);
    String fileName = Script.getFileName(this);
    Interpreter interpreter = mInterpreterConfiguration
            .getInterpreterForScript(fileName);
    if (interpreter == null || !interpreter.isInstalled()) {
        mLatch.countDown();
        if (FeaturedInterpreters.isSupported(fileName)) {
            Intent i = new Intent(this, DialogActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra(Constants.EXTRA_SCRIPT_PATH, fileName);
            startActivity(i);
        } else {
            Log
                    .e(this, "Cannot find an interpreter for script "
                            + fileName);
        }
        stopSelf(startId);
        return;
    }

    // Copies script to internal memory.
    fileName = InterpreterUtils.getInterpreterRoot(this).getAbsolutePath()
            + "/" + fileName;
    File script = new File(fileName);
    // TODO(raaar): Check size here!
    if (!script.exists()) {
        script = FileUtils.copyFromStream(fileName, getResources()
                .openRawResource(Script.ID));
    }
    copyResourcesToLocal(); // Copy all resources

    if (Script.getFileExtension(this)
            .equals(HtmlInterpreter.HTML_EXTENSION)) {
        HtmlActivityTask htmlTask = ScriptLauncher.launchHtmlScript(script,
                this, intent, mInterpreterConfiguration);
        mFacadeManager = htmlTask.getRpcReceiverManager();
        mLatch.countDown();
        stopSelf(startId);
    } else {
        mProxy = new AndroidProxy(this, null, true);
        mProxy.startLocal();
        mLatch.countDown();
        ScriptLauncher.launchScript(script, mInterpreterConfiguration,
                mProxy, new Runnable() {
                    @Override
                    public void run() {
                        mProxy.shutdown();
                        stopSelf(startId);
                    }
                });
    }
}

RpcReceiverManager getRpcReceiverManager() throws InterruptedException {
    mLatch.await();
    if (mFacadeManager==null) { // Facade manage may not be available on startup.
    mFacadeManager = mProxy.getRpcReceiverManagerFactory()
    .getRpcReceiverManagers().get(0);
    }
    return mFacadeManager;
}

@Override
protected Notification createNotification() {
    Notification notification =
        new Notification(R.drawable.script_logo_48, this.getString(R.string.loading), System.currentTimeMillis());
    // This contentIntent is a noop.
    PendingIntent contentIntent = PendingIntent.getService(this, 0, new Intent(), 0);
    notification.setLatestEventInfo(this, this.getString(R.string.app_name), this.getString(R.string.loading), contentIntent);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    return notification;
}
4

2 に答える 2

2

ここでいくつかの異なる質問があります。まず、アプリで何らかの作業を行うスクリプトは、おそらくアクティビティ内で開始するべきではありません。これは、おそらく、スクリプトの実行をアクティビティのライフサイクルに関連付ける必要がないためです。アクティビティのライフサイクル @ http://developer.android.com/reference/android/app/Activity.htmlをチェックすることをお勧めします

スクリプトの動作を制御するには、Service を使用することをお勧めします。ドキュメントの状態として

Service は、ユーザーと対話せずに実行時間の長い操作を実行したい、または他のアプリケーションが使用する機能を提供したいというアプリケーションの要求を表すアプリケーション コンポーネントです。

このサービスは通常、通知の表示とキャンセルを制御する場所でもあります。通知インテントからのアクティビティの起動モードに関する限り、これは複雑になる可能性がありますが、通常、通知内からのアクティビティの起動によって重複するアクティビティが作成されないようにする必要があります。詳細については、 @ http://developer.android.com/guide/topics/ui/notifiers/notifications.htmlをご覧ください。

于 2012-09-02T03:58:46.377 に答える
0

onRestart は onStart をトリガーするため、アプリを再起動するたびにスクリプトが再度トリガーされます。便利な Android ライフサイクル フローについては、http://www.androidjavadoc.com/1.0_r1_src/android/app/Activity.htmlをご覧ください。

これは、アクティビティがサービスをトリガーするだけで、サービスとして実装したほうがよいのではないでしょうか?

于 2012-09-02T03:59:08.233 に答える