Reputation: 13
The problem I'm currently experiencing is that I am unable to start and stop the service of another class. I was able to run the application but I couldn't get the results I wanted, as the program didn't show any errors. I thought it was something wrong with my buttons at first, so i tried using System.out.println to see if i get any feedback, in which i did. Then, i decided to try it on my other class (location.java), the System.out.Println I used did not work, which probably means its not even working.
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText input;
private Button start, stop;
private Intent intent;
public static int minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniUI();
}
private void iniUI() {
input=findViewById(R.id.input);
start=findViewById(R.id.start);
stop=findViewById(R.id.stop);
start.setEnabled(false);
stop.setEnabled(false);
start.setOnClickListener(this);
stop.setOnClickListener(this);
intent=new Intent(this, Location.class);
permission();
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.start) {
minute=Integer.parseInt(input.getText().toString());
startTracking();
}
else if(view.getId()==R.id.stop)
stopTracking();
}
private void permission(){
if(ContextCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {
start.setEnabled(true);
}
else{
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 0);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==0){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
permission();
}
}
}
private void startTracking(){
startService(intent);
buttonAlt();
}
private void stopTracking() {
stopService(intent);
buttonAlt();
}
private void buttonAlt(){
start.setEnabled(!start.isEnabled());
stop.setEnabled(!start.isEnabled());
}}
Location.java
public class Location extends Service{ private Thread thread;
private LocationManager locationManager;
private double longitude, latitude;
private String dateTime, encodeData;
public Location() {
}
@Override
public void onCreate() {
super.onCreate();
thread = new Thread(runThread);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
thread.start();
return super.onStartCommand(intent, flags, startId);
}
private Runnable runThread = new Runnable() {
@Override
public void run() {
while (true) {
try {
System.out.println("Shutting down");
long sMinutes = MainActivity.minute * 60 * 1000;
Thread.sleep(sMinutes);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Running");
sendLocation();
}
}
};
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void sendLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
@SuppressLint("MissingPermission")
android.location.Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude=location.getLongitude();
latitude=location.getLatitude();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date(System.currentTimeMillis());
dateTime=sdf.format(date);
postData();
}
private void encodeData() throws UnsupportedEncodingException {
encodeData= URLEncoder.encode("datetime", "UTF-8") + "=" + URLEncoder.encode(dateTime, "UTF-8") +
"&" + URLEncoder.encode("latitude", "UTF-8") + "=" + URLEncoder.encode(Double.toString(latitude), "UTF-8") +
"&" + URLEncoder.encode("longitude", "UTF-8") + "=" + URLEncoder.encode(Double.toString(longitude), "UTF-8");
System.out.println("Lat:"+latitude);
System.out.println("Long:"+longitude);
}
private void postData(){
try {
encodeData();
URL url=new URL("A PHP SCRIPT TO OBTAIN MY LATITUDE AND LONGITUDE");
URLConnection urlConnection=url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(encodeData);
outputStreamWriter.flush();
new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Location"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
To sum it all up, both classes showed no errors, but only the MainActivity.java was working. Is there anything i could change about it to get it to work? I think it has something to do with my startService() and stopService().
Upvotes: 0
Views: 213
Reputation: 430
It seems like in your Manifest file you have registered your service Location.java as an activity, it should work once you change that to service
<service android:name=".Location" />
instead of
<activity android:name=".Location"></activity>
Upvotes: 1