I try use Youtube API in my Android App, my app looke like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ytpv = (YouTubePlayerView) findViewById(R.id.youtubeplayer);
ytpv.initialize("this is my key", this);
et = (EditText) findViewById(R.id.eturl);
et.setOnEditorActionListener(this);
}
#Override
public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
if(arg1 == YouTubeInitializationResult.DEVELOPER_KEY_INVALID)
Toast.makeText(this, "Initialization Fail- key invalid", Toast.LENGTH_LONG).show();
else if(arg1 == YouTubeInitializationResult.SERVICE_INVALID)
Toast.makeText(this, "Initialization Fail- Service invalid", Toast.LENGTH_LONG).show();
else if(arg1 == YouTubeInitializationResult.INVALID_APPLICATION_SIGNATURE)
Toast.makeText(this, "Initialization Fail- invalid application", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player,boolean wasrestored) {
ytp = player;
Toast.makeText(this, "Initialization Success", Toast.LENGTH_LONG).show();
}
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_GO ){
if(ytp !=null){
ytp.loadVideo(et.getText().toString());
}
}
return false;
}
But when i install on my real device, I get a error : Initialization Fail- Service invalid
I have no idea to fix this error. Let me hear your idea, thank you :)
The Android YouTube API docs at:
https://developers.google.com/youtube/android/player/ includes this note:
Note: Users need to run version 4.2.16 of the mobile YouTube app (or
higher) to use the API.
I was getting a similar error until I upgraded the version of the YouTube app on my testing device.
off topic: just if someone like me stumbles on this thread looking for a SERVICE_DISABLED error, this happens when the Youtube app is disabled.
It gives SERVICE_INVALID error if your clock isn't appropriately set.
Related
I,m trying to build my first app using Android Studio.Its for Android TV. I can't figure out how to notify the user if a download fails.
It's an Android TV app so threes no status bar to display download managers progress in.The code as is displays results from any button click including ones from mainactivity2. However obviously the way I have it it displays the same message when receiving ACTION_DOWNLOAD_COMPLETE regardless of success or failure.
I've tried many methods but most assume I have a better grasp on coding than I do, or had many errors I'm not knowledgeable enough to understand, so I've been unable to incorporate them.
I did manage to get a progress bar to work but with the files being less than a megabyte, and only providing an empty progress bar when download was unsuccessfull, it was less than ideal.
I'm hoping someone can help me display failure or success.
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Your download is complete.", Toast.LENGTH_SHORT).show();
}
};
#Override
public void onClick(View view) {
registerReceiver(receiver, filter);
int id = view.getId();
if (id == R.id.myfile) {
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request1 = new DownloadManager.Request(
Uri.parse("https://myurl.zip"));
request1.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/mydir/myfile.zip");
dm.enqueue(request1);
Toast.makeText(MainActivity.this, "Your chose myfile.zip", Toast.LENGTH_SHORT).show();
} else if (id == R.id.myfile2) {
DownloadManager dm;
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request2 = new DownloadManager.Request(
Uri.parse("https://myurl2.zip"));
request2.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/mydir2/myfile2.zip");
dm.enqueue(request2);
Toast.makeText(MainActivity.this, "You chose myfile2.zip.", Toast.LENGTH_SHORT).show();
}
}
}
I thought I should post my resolution to my problem in hopes it may assist someone with a similar situation.
I ultimately abandoned DownloadManager in favor of the fast Android networking library from https://github.com/amitshekhariitbhu/Fast-Android-Networking.
I'm sure there was a way to accomplish my goal with DownloadManager but this worked for me.
Here's an Example of receiving a success or failure toast when downloading a file. Progress status is unused in this example.
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.example) {
Toast.makeText(MainActivity.this, "You chose The example.", Toast.LENGTH_SHORT).show();
AndroidNetworking.download("https://example.github.io/repository.0.3.1.zip",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/example", "/repository.0.3.1.zip")
.setTag("Download")
.setPriority(Priority.MEDIUM)
.build()
.setDownloadProgressListener(new DownloadProgressListener() {
#Override
public void onProgress(long bytesDownloaded, long totalBytes) {
}
})
.startDownload(new DownloadListener() {
#Override
public void onDownloadComplete() {
Toast.makeText(MainActivity.this, "The download is complete.", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(ANError error) {
Toast.makeText(MainActivity.this, "The download failed.", Toast.LENGTH_SHORT).show();
}
});
I'm trying to check if an app is accessing the camera (even in the background) in Android, and I'm using a service to do that.
That's my service code:
public class BackgroundService extends Service {
#Nullable
Context context;
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "Service started!", Toast.LENGTH_LONG).show();
if (isCameraUsedbyApp()){
Toast.makeText(this, "Camera is used!", Toast.LENGTH_LONG).show();
}
}
#Override
public void onDestroy() {
Toast.makeText(this, "Service stopped!", Toast.LENGTH_LONG).show();
}
public boolean isCameraUsedbyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
}
I've followed the answers to these questions:
How to check if camera is opened by any application
How to check if android.hardware.Camera is released?
But without success, in fact the service always says that camera is used, also when it isn't.
In addition, I'm trying to get the app that is using camera (if it is used), but I cannot find some references to that in Android developers docs (official one).
Can you please help me? If you need further information you've only to ask
Regards,
F.
I think each android device has an abitily to on/off auto-rotating function.
Usually you can find it in settings->display->auto-rotate on/off. How can I read this setting state from my application? How can I access to this setting value? If you can share a code snipped i'd be very appreciate it.
Hope this code snippet helps you out:-
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
if (android.provider.Settings.System.getInt(getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
Toast.makeText(getApplicationContext(), "Rotation ON", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Rotation OFF", Toast.LENGTH_SHORT).show();
}
super.onCreate(savedInstanceState);
}
Use the following code:
if (android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Rotation.this, "Rotation OFF", Toast.LENGTH_SHORT).show();
}
Try this:
public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
I have the following code in my app to initiate Map View
private boolean servicesOk() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Connect Connect to Maps", Toast.LENGTH_SHORT)
.show();
}
return false;
}
But on the check the isAvailable has the value 9, moreover in LogCat its continously repeating message: Google Play services is invalid. Cannot recover.
Can anyone please tell me, what mistake Am I making?
From the documentation 9 is an invalid/inauthentic install of google play services.
Have you made sure you downloaded and updated gps from the play store?
I am using Android 2.3.1 project to get a current location using LocationManager.GPS_PROVIDER.
This is my code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new BTILocationListener();
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
public class BTILocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
Log.d("BTILocation", "Inside onLocationChanged() ---");
if (loc != null) {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
String locationstr = "Latitude = " + latitude + " longitude = " +longitude;
Log.d("BTILocation", locationstr);
}
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
Log.w("BTILocation", "GPS is Disabled");
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
Log.w("BTILocation", "GPS is Enabled");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
The permission set I use -
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
I use open telnet connection
telnet localhost 5544
geo fix 12 22
I see following errors on logcat -
E/GpsLocationProvider( 75): native_start failed in startNavigating()
The location object returned in onLocationChanged(location loc) is always null on 2.3.1.
The same code works fine in Android 2.1 and 4.0. Please please help me solve this issue, I have tried google search but I couldn't find the solution anywhere. Thanks! I don't have Google APIs compatible with Android API level 7, does anyone know how I can download it?
It seems that it's a known problem with platform 2.3 I downloaded Google APIs for platform 2.2 and 2.1 using android SDK manager. Just had to check obsolete checkbox to be able to download the required packages.
I got the same error with 4.4.2 in API level 17. This is sporadic though, not really clear what fixes the issue. I did reboot of emulator, system all the usual stuff, nothing helped.
But I opened another emulator and that fixed them both.