我正在使用Android 2.3.5在HTC上测试以下应用程序。出于某种原因,应用程序无法在重新启动或启动手机时启动。
我需要知道我错在哪里?
BootReciever.java
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class BootupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();
/*
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
*/
// context.startActivity(new Intent(context, MainActivity.class));
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Android清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbootreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hello_android_world.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="com.example.androidbootreciever.BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
代码正在重启但不能在关机后打开手机。以下是我在logcat中收到的消息:
答案 0 :(得分:2)
您是否使用Activity扩展了BootupReceiver? 应该使用广播接收器进行扩展。
public class BootupReceiver extends BroadcastReceiver{
}
答案 1 :(得分:1)
您需要写下以下权限,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
如果您的应用程序存储在外部存储上,那么您应该提供以下意图过滤器,
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
答案 2 :(得分:1)
您的接收者:
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent receivedIntent) {
context.startActivity(new Intent(context, YourActivity.class));
}
}
您的舱单:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.receiver.AutoStartBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
祝你好运!