i am unable to get the the front facing camera (camera id 1). open cam #1 failed appears on screen when i try to run the following code.
enclosed a snippet. any help will be appreciated (i am trying it on nexus 4).
Tester.java
package com.example.cam;
import java.util.HashMap;
import java.util.Iterator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class Tester extends Activity {
private static final int[] camIds = { Camera.CameraInfo.CAMERA_FACING_BACK, Camera.CameraInfo.CAMERA_FACING_FRONT };
#SuppressLint("UseSparseArrays")
private final HashMap<Integer, Camera> camMap = new HashMap<Integer, Camera>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
protected void onResume() {
super.onResume();
if (!checkCameraHardware())
finish();
getCameras();
releaseCamera();
}
#Override
protected void onPause() {
super.onPause();
releaseCamera();
}
#Override
protected void onStop() {
super.onStop();
releaseCamera();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseCamera();
}
private final void doToast(final String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private final void getCameras() {
for (int camId : camIds)
try {
camMap.put(camId, Camera.open(camId));
doToast("open cam #" + camId + " succeeded");
} catch (Exception e) {
doToast("open cam #" + camId + " failed");
}
}
private final void releaseCamera() {
final Iterator<Integer> iter = camMap.keySet().iterator();
while (iter.hasNext())
camMap.remove(iter.next()).release();
}
private final boolean checkCameraHardware() {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return camIds.length <= Camera.getNumberOfCameras();
else
return false;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cam"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.cam.Tester"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Related
I was creating an application to test whether File.listFiles() method is working or not. To check this I made an application and I used it there but this returning null in place of an array. This is my full code please help and I have granted all permissions for android 11
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="#style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
deleteFiles(view);
}
});
}
private void deleteFiles(View v) {
File file = new File("/storage/emulated/0/DCIM/");
if (file.isDirectory())
{
File[] files;
files = file.listFiles();
int a = files.length;
for (int i = 0; i < files.length; i++)
{
new File(file, files[i].getName()).delete();
}
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), (CharSequence) file, Toast.LENGTH_LONG).show();
}
}
}
Hi I'm integrating Chartboost in my game but the ads aren't showing in live and testing mode. I followed this tutorial in exact manner and there is no error in my code https://github.com/itsabhiaryan/AdService-LibGDX
In https://github.com/itsabhiaryan/AdService-LibGDX/blob/master/android/src/com/its/adservice/AndroidLauncher.java line 34 I changed to ad=new ChartBoostHelper(this); I removed the onSaveInstanceState and onRestoreInstanceState methods and also I made other changes. I called the show ads method in the right place but it does not show.
I registered Chartboost and added my appId and signature accordingly and also set the setting to landscape and interstitial ads. I have modified the AndroidManifest file as in the tutorial and added the jar file in /android/libs folder. Am I missing something or doing something wrong?
This is my android/AndroidLauncher.java and the ChartBoostHelper and the Ad class is the same as in the github link.
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class AndroidLauncher extends AndroidApplication implements ActionResolver {
private Ad ad;
public RelativeLayout layout;
View gameView;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useAccelerometer = false;
config.useCompass = false;
config.useImmersiveMode = true;
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
View gameView=initializeForView(new MyGame(this), config);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
gameView.setLayoutParams(gameViewParams);
layout.addView(gameView);
ad=new ChartBoostHelper(this);
ad.embedView(layout);
setContentView(layout);
#Override
public void onResume() {
super.onResume();
ad.resume();
}
#Override
public void onPause() {
ad.pause();
super.onPause();
}
#Override
public void onDestroy() {
ad.destroy();
super.onDestroy();
}
#Override
protected void onStart() {
super.onStart();
ad.start();
}
#Override
protected void onStop() {
super.onStop();
ad.stop();
}
#Override
public void showOrLoadInterstitial() {
ad.showOrLoadInterstitial();
}
#Override
public boolean showVideoAd(boolean isRewarded) {
return ad.showVideoAd(isRewarded);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
This is the core/ActionResolver.java
public interface ActionResolver {
void showOrLoadInterstitial();
boolean showVideoAd(boolean isRewarded);
}
This is the part where I call showOrLoadInterstitial(); in the render method inside of core/GameScreen.java
if((touchPos.x >= camera.position.x-(replaybutton.getWidth()/2 + 50) && touchPos.x <= (camera.position.x - (replaybutton.getWidth()/2 + 50))+replaybutton.getWidth()) && (touchPos.y>=55 && touchPos.y<=55+replaybutton.getHeight())){
if(SplashScreen.adcount >= 4 && prefs.getBoolean("ADBLOCK") == false){
game.actionResolver.showOrLoadInterstitial();
SplashScreen.adcount = 0;
}
game.setScreen(new MainMenu(game,this.asset));
dispose();
}
Here is my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robot1gamesfree.game"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="com.robot1gamesfree.game.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="sensorLandscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name="com.chartboost.sdk.CBImpressionActivity"
android:excludeFromRecents="true"
android:hardwareAccelerated="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
android:configChanges="keyboardHidden|orientation|screenSize" />
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
I am trying to create a webview app. I tried to add the access to manifest but it didn't change anything. On the chrome browser the camera works, I can't understand what should I do.
1. This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="i.astiapp.astiappadmindev">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<!-- My Location -->
<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:screenOrientation="portrait"
android:name=".MainActivity">
</activity>
<activity
android:hardwareAccelerated="true"
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /></intent-filter>
</activity>
</application>
</manifest>
2. Main Activity
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener{
private ProgressBar chromeProgressBar;
WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
//chromeProgressBar = (ProgressBar) findViewById(R.id.progressBarChrome);
//Settings
WebSettings webSettings = myWebView.getSettings();
myWebView.getSettings().setTextSize(WebSettings.TextSize.NORMAL);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
}
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
//webSettings.setLoadsImagesAutomatically(true);
//inizialize client
//load website by URL
myWebView.loadUrl("https://www.voyage-app.eu/asti-app/admin/index.php");
//register token for notification
// this.onStart();
chromeProgressBar = (ProgressBar) findViewById(R.id.progressBar);
myWebView.setWebChromeClient(new WebChromeClient() {
// Grant permissions for cam
#Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(request.getResources());
}
}
});
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
chromeProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
chromeProgressBar.setVisibility(View.GONE);
}
});
}
#Override
public void onUpdateProgress(int progressValue) {
chromeProgressBar.setProgress(progressValue);
if (progressValue == 100) {
chromeProgressBar.setVisibility(View.INVISIBLE);
}
}
public class WebViewController extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed()
{
if(myWebView.canGoBack())
myWebView.goBack();
else
super.onBackPressed();
}
}
I don't think I should post the PHP file since on the chrome browser the page works. It happens like this:
You enter the page
Chrome browers asks you to give you the permission to the camera
The camera starts working
Fixed it. I had to add the permession to the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
List<String> permissions = new ArrayList<String>();
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
}
}
But for this to work I had to add the manifest file too:
import android.Manifest;
In a special app for android i need to make a service that play a sound for hours and control it by my service ... but sound player`s code does not work in any method of my service class :((
my player class that extend from Service class :
package org.tooj;
import org.qtproject.example.Book.R;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class Player extends Service {
MediaPlayer Player;
#Override
public void onCreate() {
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
//my player
public int onStartCommand(Intent intent, int flags, int startId) {
Player = MediaPlayer.create(ToojActivity.getInstance(),R.ra w.alfa);
Player.setLooping(true);
Player.setVolume(1,1);
Player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
Player.stop();
Player.release();
}
#Override
public void onLowMemory() {
}
}
my main activity :
package org.tooj;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import org.qtproject.qt5.android.bindings.QtActivity;
public class ToojActivity extends QtActivity{
private static ToojActivity instance;
private boolean _transparentStatusBar = false;
private boolean _transparentNavigationBar = false;
public ToojActivity(){
instance = this;
Log.i("ToojActivity", "ctor");
}
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent svc=new Intent(this, Player.class); // call service
startService(svc);
public static ToojActivity getInstance() {
return ToojActivity.instance;
}
public void SetStatusBarIsTransparent(boolean value) {
_transparentStatusBar = value;
}
public void GetNavigationBarIsTransparent(boolean value) {
_transparentNavigationBar = value;
}
public boolean GetStatusBarIsTransparent() {
return _transparentStatusBar;
}
public boolean GetNavigationBarIsTransparent() {
return _transparentNavigationBar;
}
}
and manifest ...
<?xml version='1.0' encoding='utf-8'?>
<manifest package="org.qtproject.example.Book" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="Book">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
android:name="org.tooj.ToojActivity"
android:label="Book"
android:screenOrientation="unspecified"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<meta-data android:name="android.app.lib_name" android:value="Book"/>
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="#array/qt_sources"/>
<meta-data android:name="android.app.repository" android:value="default"/>
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="#array/qt_libs"/>
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="#array/bundled_libs"/>
<!-- Deploy Qt libs as part of package -->
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="#array/bundled_in_lib"/>
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="#array/bundled_in_assets"/>
<!-- Run with local libs -->
<meta-data android:name="android.app.use_local_qt_libs" android:value="1"/>
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
<meta-data android:name="android.app.load_local_libs" android:value="plugins/platforms/android/libqtforandroid.so:plugins/bearer/libqandroidbearer.so:lib/libQt5QuickParticles.so:plugins/mediaservice/libqtmedia_android.so:lib/libQt5MultimediaQuick_p.so"/>
<meta-data android:name="android.app.load_local_jars" android:value="jar/QtAndroid.jar:jar/QtAndroidAccessibility.jar:jar/QtAndroid-bundled.jar:jar/QtAndroidAccessibility-bundled.jar:jar/QtAndroidBearer.jar:jar/QtAndroidBearer-bundled.jar:jar/QtMultimedia.jar:jar/QtMultimedia-bundled.jar"/>
<meta-data android:name="android.app.static_init_classes" android:value="org.qtproject.qt5.android.multimedia.QtMultimediaUtils:org.qtproject.qt5.android.multimedia.QtMultimediaUtils"/>
<!-- Messages maps -->
<meta-data android:value="#string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
<meta-data android:value="#string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
<meta-data android:value="#string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
<!-- Messages maps -->
<!-- Splash screen -->
<!--
<meta-data android:name="android.app.splash_screen_drawable" android:resource="#drawable/logo"/>
-->
<!-- Splash screen -->
<!-- Background running -->
<!-- Warning: changing this value to true may cause unexpected crashes if the
application still try to draw after
"applicationStateChanged(Qt::ApplicationSuspended)"
signal is sent! -->
<meta-data android:name="android.app.background_running" android:value="false"/>
<!-- Background running -->
</activity>
<service android:enabled="true" android:name=".Player" />
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
Remove the comment if you do not require these default permissions. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
Remove the comment if you do not require these default features. -->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
</manifest>
I am currently trying to implement an app that has a service running until the user explicitly ends it via the app.
The service runs even when I exit the app(which is ok)
But when I enter the app again and try to stop the service, the app stops running.
What am I doing wrong?
This is the service code
package com.mac.service;
import android.app.Service;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
private Handler handler = new Handler();
private long timeAndSpace = 5000;
Thread t;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
}
#Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
#Override
public void onStart(Intent intent, int startid) {
}
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT)
.show();
final Runnable r = new Runnable() {
public void run() {
Location l = getMine();
Toast.makeText(
getApplicationContext(),
String.valueOf(l.getLatitude()) + " "
+ String.valueOf(l.getLongitude()),
Toast.LENGTH_SHORT).show();
handler.postDelayed(this, timeAndSpace);
}
};
handler.postDelayed(r, timeAndSpace);
return START_STICKY;
}
private Location getMine() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
return myLocation;
}
}
and this the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mac.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<uses-permission android:name="com.mac.maps2demo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.mac.service.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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<service
android:name="com.mac.service.MyService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>