-1

Eclipse と Java の開発は初めてで、起動時にアクティビティを開始しようとしています。このトピックについて議論している複数のスレッドを読んだことがありますが、起動時にアプリケーションを起動することはできましたが、クラッシュします。

これは私のコードです:

AndroidManifest.xml :

<!--
Below the <manifest> opening tag:
-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!--
Inside the <application> tag:
-->
<receiver android:name="com.example.Autostart">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />  
        </intent-filter>  
</receiver>

<service android:name="com.example.service" android:enabled="true" />

Autostart.java :

package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Autostart extends BroadcastReceiver{
    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
        Intent intent = new Intent(arg0, service.class);
        arg0.startService(intent);
    }
}

service.java :

package com.example;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class service extends Service{

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

    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(),checker.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    }

}

私のチェッカー クラスには、いくつかの設定値を変更する OnCreate 関数があります。

電話を起動したときに表示される唯一のことは、「YourAppName が動作を停止しました」ということです。これは、アプリケーションがクラッシュしたことを意味します。トースト メッセージが表示されません。

通常、アプリケーションを開いて、起動時に書き込む必要がある設定を読み取っても、何もありません。

4

2 に答える 2

0

問題は、自分のアクティビティをマニフェスト ファイルに宣言していなかったことです。

于 2013-09-15T07:45:55.583 に答える
0

マニフェストを次のように変更して試してください。

  <receiver android:enabled="true" android:name=".Autostart"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </receiver>
于 2013-09-12T11:18:11.167 に答える