Search code examples
androidservicebroadcastreceiverandroid-activity

i am trying to start the activity from the service but the application getting force close. I am starting the service from the broadcast receiver


the code for the broadcast receiver is

public class MyBroadcastreceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    Intent startServiceIntent = new Intent(context, MyService.class);
    startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(startServiceIntent);

   }
 }

the code for the service is

public class MyService extends Service {

 String tag="TestService";
 @Override
  public void onCreate() {

     Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
  // Log.i(tag, "Service created...");
    }

   @Override
   public void onStart(Intent intent, int startId) {      
   super.onStart(intent, startId);  
   Intent dialogIntent = new Intent(getBaseContext(), LocationStat.class);
   dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   getApplication().startActivity(dialogIntent);
   }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

   Toast.makeText(this,"task perform in service",Toast.LENGTH_LONG).show();
  /* startActivity(new Intent(MyService.this,LocationStat.class));*/
     return super.onStartCommand(intent, flags, startId);
   }
   @Override
   public void onDestroy() {
   super.onDestroy();
   Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
   }

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

and the activity that i am trying to start from the service is

public class LocationStat extends Activity {
double logi;
double lat;
long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Millisecon
Location loc;
LocationManager manager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    manager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    manager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );
   Toast.makeText(this,"activity created...",Toast.LENGTH_LONG).show();
   loc=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

   String s;
   if(loc!=null)
   {
     logi=loc.getLongitude();
     lat=loc.getLatitude();
      s="Lat:" + lat + "\nLong:" + logi;

   }
   else
   {
    s ="no location found";
   }

   Toast.makeText(LocationStat.this,"Your Current Position is:\n" +
           s,Toast.LENGTH_LONG).show();
   webcall(logi,lat);
   //getLoc();

}
public void webcall(double logi,double lat)
    {
        InputStream is=null;
        String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("logitude",Double.toString(logi)));
        nameValuePairs.add(new BasicNameValuePair("latitude",Double.toString(lat)));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/location.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }catch(Exception e){
                Toast.makeText(LocationStat.this,"Error in http connection "+e.toString(),Toast.LENGTH_LONG).show();
        }
        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();

                 result=sb.toString();




                Toast.makeText(LocationStat.this,result,Toast.LENGTH_LONG).show();
        }catch(Exception e){
                Toast.makeText(LocationStat.this,"Error converting result       "+e.toString(),Toast.LENGTH_LONG).show();
        }

the manifest look like this

<service android:enabled="true" android:name=".MyService">
            <intent-filter >
                <action android:name="com.android.trace.MyService"/>

            </intent-filter>
        </service>

        <receiver android:name="com.android.trace.MyBroadcastReceiver">  
            <intent-filter>  
              <action android:name="android.intent.action.BOOT_COMPLETED" />  
             </intent-filter>  
        </receiver> 


    <activity
        android:name=".LocationStat"
        android:label="@string/title_activity_location_stat" >
        <intent-filter>
             <action android:name="android.intent.action.MAIN" />  

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
 <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.RECEIVE_BOOT_COMPLETED" /> 

the logcat is:

08-28 06:47:07.388: E/AndroidRuntime(265): FATAL EXCEPTION: main
08-28 06:47:07.388: E/AndroidRuntime(265): java.lang.RuntimeException: Unable to instantiate receiver com.android.trace.MyBroadcastReceiver: java.lang.ClassNotFoundException: com.android.trace.MyBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.android.trace-1.apk]
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.app.ActivityThread.access$3200(ActivityThread.java:125)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.os.Looper.loop(Looper.java:123)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-28 06:47:07.388: E/AndroidRuntime(265):  at java.lang.reflect.Method.invokeNative(Native Method)
08-28 06:47:07.388: E/AndroidRuntime(265):  at java.lang.reflect.Method.invoke(Method.java:521)
08-28 06:47:07.388: E/AndroidRuntime(265):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-28 06:47:07.388: E/AndroidRuntime(265):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-28 06:47:07.388: E/AndroidRuntime(265):  at dalvik.system.NativeStart.main(Native Method)
08-28 06:47:07.388: E/AndroidRuntime(265): Caused by: java.lang.ClassNotFoundException: com.android.trace.MyBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.android.trace-1.apk]
08-28 06:47:07.388: E/AndroidRuntime(265):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-28 06:47:07.388: E/AndroidRuntime(265):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-28 06:47:07.388: E/AndroidRuntime(265):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-28 06:47:07.388: E/AndroidRuntime(265):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
08-28 06:47:07.388: E/AndroidRuntime(265):  ... 10 more

i have taken all the necessary permissions in the manifest for broadcast receiver and service but when the application start's at boot up of device and force close immediately

please help me out, thanks in advance


Solution

  • use com.android.trace.MyBroadcastreceiver instead of com.android.trace.MyBroadcastReceiver in manifast.xml for Registering Broadcast Receiver.