Is printing not allowing within a service in android? - java

I wrote a service in android app to read a pdf file and send to the print using print manager. but it not allowing to print, and it says " IllegalStateException : Can print only from an activity".IS this a problem of newer android versions?
This is the cade
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = new PdfDocumentAdapter(pathPdfRead);
String jobName = getString(R.string.app_name) + newDoc;
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
I'm expecting some answer. Is this possible in Android 11 or higher versions?

The exception is self-explanatory:
IllegalStateException : Can print only from an activity
So if you do such implementation in a Service, the this in this line is not an Activity Context:
PrintManager printManager = (PrintManager)this.getSystemService(Context.PRINT_SERVICE);
You must have an Activity Context in order to make it work.

Related

FLAG_ACTIVITY_NEW_TASK error integrating android native library in react native

I'm trying to integrate a native payment library in a react native application.
The library requires an view setup like this to process the pay result.
The library raises next error
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I don't know if my configuration is wrong, the native library demo application works fine. :(
The pay response intent tries load the activity but apparently it can't find it.
Update
The library function that fails is:
String appId = context.getPackageName() + "." + callbackSchema;
Intent intent = new Intent(appId);
intent.putExtra(ConstantUtil.RETURNED_JSON_KEY,json);
context.startActivity(intent); // <- this call raises the error
Update
Finally I made it work. I was passing context like that:
getReactApplicationContext()
I solved it passing context like that:
getCurrentActivity()
In debug, I got this values:
but the error still raising :(
Any idea?
Finally I made it work. I was passing context like that:
getReactApplicationContext()
I solved it passing context like that:
getCurrentActivity()

My device will flash back to the background after the multilingual handover jump

BaseActivity.java
Language switching
Intent
OnClickListener
Error demo
No error message was prompted.
You are using old style of language switching which were deprecated and removed after releasing android 8, It completely get removed. However, if you want to use your application for android prior to 26, you have to change your code on language switching Image as follow:
Configuration config = new Configuration(resources.getConfiguration());
New instance of Configuration have to be passed to resources.updateConfiguration();. above line will solve your problem.
On the other hand if you want to publish your application for higher api level, follow link below:
#Override
protected void attachBaseContext(Context newBase){
Configuration configuration = new Configuration(newBase.getResoureces().getConfiguration());
configuration.setLocale(/* selected locale which you have to get from user or your app configuration*/);
super.attachBaseContext(newBase.createConfigurationContext(configuration));
}
Override above method for each activity which you have declared;

App crashing after update FragmentList from loader using support library

I have a loader that uses the Support Library v4, it is being used to carry the loading of a ListView in an Activity that contains two fragments, one containing the ListView (being it an extension of ListFragment) and the other carrying a button (which can be clicked while the loader is doing the work).
The implementation is pretty similar to the one available at the Android documentation of the AsyncTaskLoader, which is also creating a ListView trough the loader, with the exception of the monitoring part, where my implementation does not need the monitoring of changes: http://developer.android.com/reference/android/content/AsyncTaskLoader.html
As the app has support to API level 8, I am using the FragmentActivity::getSupportLoaderManager method to start the loader as advised in the documentation in order to keep the support.
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.
Being the loader started from the Fragment, I had to use the ListFragment::getActivity method to call the method FragmentActivity::getSupportLoaderManager, resulting in the following code being used to start the loader:
getActivity().getSupportLoaderManager().initLoader(0, null, this).forceLoad();
The app is running fine with APIs higher than 8, but on level 8 it is crashing when the loader tries to renders the list on UI after loading (Loader::onLoadFinished method).
Debugging I found that it is crashing at the moment that the ArrayAdapter<>::addAll method is being called on the adapter, which confirms that the problem is on the rendering of the UI. At that moment, the app is thrown to the SamplingProfilerIntegration class where something related to a snapshot is trying to be done at a static part of the class:
/** Whether or not a snapshot is being persisted. */
private static final AtomicBoolean pending = new AtomicBoolean(false);
static {
samplingProfilerMilliseconds = SystemProperties.getInt("persist.sys.profiler_ms", 0);
samplingProfilerDepth = SystemProperties.getInt("persist.sys.profiler_depth", 4);
if (samplingProfilerMilliseconds > 0) {
File dir = new File(SNAPSHOT_DIR);
dir.mkdirs();
// the directory needs to be writable to anybody to allow file writing
dir.setWritable(true, false);
// the directory needs to be executable to anybody to allow file creation
dir.setExecutable(true, false);
if (dir.isDirectory()) {
snapshotWriter = Executors.newSingleThreadExecutor(new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r, TAG);
}
});
enabled = true;
Log.i(TAG, "Profiling enabled. Sampling interval ms: "
+ samplingProfilerMilliseconds);
} else {
snapshotWriter = null;
enabled = true;
Log.w(TAG, "Profiling setup failed. Could not create " + SNAPSHOT_DIR);
}
} else {
snapshotWriter = null;
enabled = false;
Log.i(TAG, "Profiling disabled.");
}
}
It could be related to the specific behavior of the UI rendering on prior to Honeycomb versions as stated in the documentation, but I can not think of what.
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
Prior to Honeycomb (3.0), an activity's state was saved before pausing. Fragments are a significant amount of new state, and dynamic enough that one often wants them to change between pausing and stopping. These classes throw an exception if you try to change the fragment state after it has been saved, to avoid accidental loss of UI state. However this is too restrictive prior to Honeycomb, where the state is saved before pausing. To address this, when running on platforms prior to Honeycomb an exception will not be thrown if you change fragments between the state save and the activity being stopped. This means that in some cases if the activity is restored from its last saved state, this may be a snapshot slightly before what the user last saw.
I found that the support libraries v4 and v7, which are the being imported to the project, does not support the method ArrayAdapter<>::addAll() and this was making the app crash.
This question is related to this issue and the solution presented was suitable to address my problem:
ListViews - how to use ArrayAdapter.addAll() function before API 11?
So, the solution was to implement my own version of the ArrayAdapter class, so that the prior to Honeycomb versions of Android could use it.

Invalid unknown request error: Cannot determine request type. Is this ad unit id correct?

After updating to "new abMob", it appears to have this problem...
use this code, but nothing displays:
adView = new AdView(this, AdSize.BANNER, "pub-xxxxxxxxxxxxxxxx");
and got warning:
Invalid unknown request error: Cannot determine request type. Is your
ad unit id correct?
What's wrong?
Using android emulator, admob 6.4.1
In my case, the ad unit id was incorrect in AdMob app management section! There was a ~ instead of / in the id. I fixed it by viewing the GoogleService info plist and found the correct unit id.
Change:
ca-app-pub-XXXXXXXXXX~XXXXXXXXXX
to:
ca-app-pub-XXXXXXXXXX/XXXXXXXXXX
The adUnit Ids in the new Admob console are of the form:
ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYYY
This is what your problem is.
Go check your Admob console and update your app to reflect that.
First you have to go to www.admob.com. There sign in with your account. Then klick at "Monetize" next to "Home". There you have to add your app and set the settings for your advertisement. After that, the website is asking you to send you an e-mail with your ad unit id, you have to put in your strings.xml file instead of the exsample id. Hope this helps.
#if UNITY_ANDROID
string adUnitId = " ca-app-pub-8572140050384888/1853336688 "; //no spaces are allowed before and after admob-id..
Change to :
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-8572140050384888/1853336688";
android java admobunity
You don't need to have an app already on the market to get a Publisher ID for a new app. In the Android Package URL field that's on the "Add Site/App" form, just enter "market://details?id=your.package.name". You'll of course want to replace the 'your.package.name' with your app's actual package name.
The viewWidth warnings are fine and you will likely still see them after inserting your Publisher ID.
Refer below links..
How to get Google's example code working for AdMob 4.1.0 using an Android device
AdMob 4.3.1 adds not showing up (No AdMob ID yet (just for test), can't get ViewWidth)
Try this format
GADBannerView * bannerView_;
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView_.adUnitID=#"ca-app-pub-5250422073333775/6825580043"; //Id for Top Banners
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];

turn off android camera in flashcs6 adobe air

Hi guys
i need turn off camera in android app created by adobe air 3 in flash cs6 .
i used this code for turn off camera :
videoDetect.attachCamera(null);
videoCapture.attachCamera(null);
but when run in my device , error :
An error has occurred in sub: settingm_camera1_ready
(java line: 464)
java.lang.IllegalStateException:The specified child already has a parent.you must call removeView() on the child s parent first.
plz help .
tanx
try the following:
video.attachCamera(null);
video.clear();
removeChild(_video);
camera = null;

Categories

Resources