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.
I have wrote broadcast receiver method and in mainfest xml even set the receiver and permission I am writing the following code but I am getting runtime error, I even tried to remove the activity and run but its causing the error . please suggest me some solution.
我已经编写了广播接收器方法,在mainfest xml中,甚至设置了接收者和权限,我正在编写下面的代码,但是我得到了运行时错误,我甚至试图删除活动并运行,但是它导致了错误。请给我一些解决办法。
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
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:
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
3 个解决方案
#1
0
For everyone with the question:
对于每个有问题的人:
How to receive the broadcast without the manifest and declare it programmatically in an activity.
如何在没有清单的情况下接收广播,并在活动中以编程方式声明它。
- write the receiver as an inner class of your activity.
- 将接收方作为活动的内部类。
- register the receiver in the onResume method of the activity
- 在活动的onResume方法中注册接收方。
- unregister receiver in the onPause method
- 在onPause方法中取消注册接收器。
i.e.
即。
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();
}
}
To send a Broadcast:
发送一个广播:
final Intent broadcast = new Intent(Simpleclass.ACTION_BROADCAST);
getApplicationContext().sendBroadcast(broadcast);
#2
3
You have to write your Receiver in its own class. Do not write it as a Field in an Activity.
你必须在自己的课堂上写你的接收器。不要把它写为活动中的字段。
If you want to register a receiver in the Manifest file it have to be in its own file. Create a new class that extends BroadcastReceiver
in a new file. Then use the name of this class as the receiver name in the manifest instead of MainActivity.
如果您想在清单文件中注册一个接收器,它必须在它自己的文件中。创建一个新类,将广播接收器扩展到一个新文件中。然后,在清单中使用这个类的名称作为接收方名称,而不是MainActivity。
#3
0
if you are declaring a receiver in manifest than it must be inside its own java file.Here you are creating the instance of receiver in activity programetically so either you register it programetically by registerReceiver method or make it in another file and give name of that file in android:name of your receiver tag.
如果您在清单中声明一个接收者,那么它必须在它自己的java文件中。这里,你在活动程序中创建了接收者的实例,因此你可以用registerReceiver方法在程序上注册它,或者在另一个文件中进行注册,并在android中给这个文件命名:你的接收者标签的名称。
#1
0
For everyone with the question:
对于每个有问题的人:
How to receive the broadcast without the manifest and declare it programmatically in an activity.
如何在没有清单的情况下接收广播,并在活动中以编程方式声明它。
- write the receiver as an inner class of your activity.
- 将接收方作为活动的内部类。
- register the receiver in the onResume method of the activity
- 在活动的onResume方法中注册接收方。
- unregister receiver in the onPause method
- 在onPause方法中取消注册接收器。
i.e.
即。
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();
}
}
To send a Broadcast:
发送一个广播:
final Intent broadcast = new Intent(Simpleclass.ACTION_BROADCAST);
getApplicationContext().sendBroadcast(broadcast);
#2
3
You have to write your Receiver in its own class. Do not write it as a Field in an Activity.
你必须在自己的课堂上写你的接收器。不要把它写为活动中的字段。
If you want to register a receiver in the Manifest file it have to be in its own file. Create a new class that extends BroadcastReceiver
in a new file. Then use the name of this class as the receiver name in the manifest instead of MainActivity.
如果您想在清单文件中注册一个接收器,它必须在它自己的文件中。创建一个新类,将广播接收器扩展到一个新文件中。然后,在清单中使用这个类的名称作为接收方名称,而不是MainActivity。
#3
0
if you are declaring a receiver in manifest than it must be inside its own java file.Here you are creating the instance of receiver in activity programetically so either you register it programetically by registerReceiver method or make it in another file and give name of that file in android:name of your receiver tag.
如果您在清单中声明一个接收者,那么它必须在它自己的java文件中。这里,你在活动程序中创建了接收者的实例,因此你可以用registerReceiver方法在程序上注册它,或者在另一个文件中进行注册,并在android中给这个文件命名:你的接收者标签的名称。