Problem: AutoStart On Boot Up causing classcastexception..
I am doing Location based application , I want to run the app whenever the phone bootup,So that my app will be running continuously, I just want to start the service as soon as the phone loads all the service.
我已经编写了广播接收方法,并且在mainfest xml中甚至设置了接收方和权限 我正在编写以下代码,但我收到运行时错误,我甚至试图删除活动并运行,但它导致错误。请给我一些解决方案。
MainFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.omkar_gpslocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<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.omkar_gpslocation.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.omkar_gpslocation.Activity_Settings"
android:label="@string/title_activity_activity__settings" >
</activity>
<receiver android:enabled="true" android:name="com.example.omkar_gpslocation.MainActivity"
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>
</application>
</manifest>
MainActivity
private BroadcastReceiver MyReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent("com.example.omkar_gpslocation");
context.startService(serviceIntent);
}
};
logcat的:
02-04 13:21:08.462: E/AndroidRuntime(503): FATAL EXCEPTION: main
02-04 13:21:08.462: E/AndroidRuntime(503): java.lang.RuntimeException: Unable to instantiate receiver com.example.omkar_gpslocation.MainActivity: java.lang.ClassCastException: com.example.omkar_gpslocation.MainActivity
02-04 13:21:08.462: E/AndroidRuntime(503): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1873)
02-04 13:21:08.462: E/AndroidRuntime(503): at android.app.ActivityThread.access$2400(ActivityThread.java:155)
02-04 13:21:08.462: E/AndroidRuntime(503): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1049)
02-04 13:21:08.462: E/AndroidRuntime(503): at android.os.Handler.dispatchMessage(Handler.java:130)
02-04 13:21:08.462: E/AndroidRuntime(503): at android.os.Looper.loop(SourceFile:351)
02-04 13:21:08.462: E/AndroidRuntime(503): at android.app.ActivityThread.main(ActivityThread.java:3820)
02-04 13:21:08.462: E/AndroidRuntime(503): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 13:21:08.462: E/AndroidRuntime(503): at java.lang.reflect.Method.invoke(Method.java:538)
02-04 13:21:08.462: E/AndroidRuntime(503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
02-04 13:21:08.462: E/AndroidRuntime(503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
02-04 13:21:08.462: E/AndroidRuntime(503): at dalvik.system.NativeStart.main(Native Method)
02-04 13:21:08.462: E/AndroidRuntime(503): Caused by: java.lang.ClassCastException: com.example.omkar_gpslocation.MainActivity
02-04 13:21:08.462: E/AndroidRuntime(503): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1864)
02-04 13:21:08.462: E/AndroidRuntime(503): ... 10 more
答案 0 :(得分:3)
您必须在自己的班级中编写您的Receiver。不要将其写为活动中的字段。
如果要在Manifest文件中注册接收器,则必须在其自己的文件中。创建一个在新文件中扩展BroadcastReceiver
的新类。然后使用此类的名称作为清单中的接收者名称,而不是MainActivity。
答案 1 :(得分:0)
如果你在清单中声明一个接收器,那么它必须在它自己的java文件中。这里你是以编程方式在活动中创建接收器的实例,所以要么通过registerReceiver方法以编程方式注册它,要么在另一个文件中创建并给出名称android中的那个文件:接收者标签的名称。
答案 2 :(得分:0)
对于有问题的每个人:
如何在没有广播的情况下接收广播 清单并以编程方式在活动中声明。
即
public class Simpleclass extends Activity
{
public final String ACTION_BROADCAST = "com.example.intent.action.something";
private aBroadcastReceiver mReceiver;
private class aBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
foo();
//do something
}
}
//[...]
@Override
public void onPause()
{
getApplicationContext().unregisterReceiver(mReceiver);
super.onPause();
}
@Override
public void onResume()
{
final IntentFilter intentFilter = new IntentFilter(mReceiver);
mReceiver= new aBroadcastReceiver();
getApplicationContext().registerReceiver(mReceiver, intentFilter);
super.onResume();
}
}
发送广播:
final Intent broadcast = new Intent(Simpleclass.ACTION_BROADCAST);
getApplicationContext().sendBroadcast(broadcast);