Reputation: 11
I am trying to figure out some code that I found on stack overflow, and really struggling to get it working, so I am turning to the geniuses on here to ask for help. Hopefully Ive put enough information in.
The code doesn't return an errors in eclipse
, however when I deploy to the emulator crashes the app with the following error in LogCat.
Unable to instantiate activity ComponentInfo{com.simon.startAppService2/com.simon.startAppService2.StartAppService2Activity}:java.lang.ClassCastException:com.simon.startAppService2.StartAppService2Activitycannot be cast to android.app.Activity
Here is the code for StartAppService2Activity
package com.simon.startAppService2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class StartAppService2Activity extends BroadcastReceiver
{
public static String trigger_message="";
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
trigger_message=msgs[i].getMessageBody().toString();
str += trigger_message;
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
if(trigger_message.equals("dx"))
{
Toast.makeText(context, "I am triggered",Toast.LENGTH_LONG).show();
///////////////////////////
//i want to start here
//////////////////////////
//MainScreenActivity.trigger="Now";
// Intent i = new Intent(context,GPS.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
//start activity
Intent i = new Intent();
i.setClassName("com.simon.startAppService2", "GPS.class");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else
{
Toast.makeText(context, "I am not triggered, Bbyz!!!",Toast.LENGTH_LONG).show();
}
}
}
}
Here is the code for GPS class
package com.simon.startAppService2;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.TextView;
import android.widget.Toast;
public class GPS extends Activity implements LocationListener{
TextView latitude,logitude;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
Toast.makeText(this, "i m started", Toast.LENGTH_LONG);
latitude = (TextView) findViewById(R.id.txtLat);
logitude = (TextView) findViewById(R.id.txtLongi);
latitude.setText("Loading...");
logitude.setText("Loading...");
}
String LATTITUDE;
String LOGITUDE;
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lag = location.getLongitude();
LATTITUDE = Double.toString(lat);
LOGITUDE = Double.toString(lag);
latitude.setText(LATTITUDE);
logitude.setText(LOGITUDE);
SmsManager sm = SmsManager.getDefault();
// // here is where the destination of the text should go
String number = "5554";
sm.sendTextMessage(number, null, "latitude="+latitude.getText()+"\nlongitude="+logitude.getText(), null, null);
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
// /** Register for the updates when Activity is in foreground */
// @Override
// protected void onResume()
// {
// super.onResume();
// lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
// }
//
// /** Stop the updates when Activity is paused */
// @Override
// protected void onPause() {
// super.onPause();
// lm.removeUpdates(this);
// }
}
And here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simon.startAppService2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".StartAppService2Activity"
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=".GPS"/>
</application>
</manifest>
Here is my main.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/txtLat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/txtLongi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
As I mentioned it was code I found on StackOverflow
for another user, which looks to be doing what I am looking for, so any help/direction that anyone can provide would be hugely appreciated.
Many thanks
Simon
Upvotes: 1
Views: 458
Reputation: 24012
This might be because the context in Intent is StartAppService2Activity
which is not extending android.app.Activity
Upvotes: 0
Reputation: 24496
Yes, txtLat
and txtLongi
is EditText
How it will be find by as TextView
So, option 1 you can change those EditText
to be TextView
in your main.xml
layout.
<TextView
android:id="@+id/txtLat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="@+id/txtLongi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
Otherwise, You can identify those values as EditText
and keep those are EditText
in your main.xml
file.
EditText latitude,logitude;
latitude = (EditText) findViewById(R.id.txtLat);
logitude = (EditText) findViewById(R.id.txtLongi);
Upvotes: 1