• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Calling an activity from class PintService

dropvid

Lurker
Apr 25, 2019
2
0
Italy
I created a class that extends PrintService .

I use the system dialog to print , everything is fine I see the service in print.

In onPrintJobQueued method I have this :

@override
protected void onPrintJobQueued(PrintJob printJob) {
printJob.start();

Log.d(TAG, "queued: " + printJob.getInfo());

Intent i = new Intent(getApplicationContext(), PdfWiever.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String file = "file:///storage/emulated/0/Download/"+printJob.getDocument().getInfo().getName();
i.putExtra("data", file);
try {
printJob.complete();
Log.d(TAG, "queued: " + printJob.getInfo());
startActivity(i);
}catch (Exception e){
e.printStackTrace();
}

}

I'm not getting any errors in both stackTrace and logcat. It's just that the business doesn't open.

I've seen other answers but I'm confused, in some I've read that there is a need for an interface to communicate between service and activity.

Or am I getting my intent call wrong?

I state that my activity is not running it is the service that must call it and put it into execution.

my AndroidManifest :

<activity
android:name="soget.com.it.bluetooth.sdk.PdfWiever"
android:exported="true"
android:label="SogetServizioStampa">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" android:scheme="file" />
<data android:scheme="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" android:scheme="file" />
<data android:scheme="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" android:scheme="file" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" android:scheme="file" />
<data android:mimeType="*/*" android:scheme="mailto" />
</intent-filter>
</activity>
<service
android:name="soget.com.it.bluetooth.sdk.PdfPrint"
android:permission="android.permission.BIND_PRINT_SERVICE">
<intent-filter>
<action android:name="android.printservice.PrintService" />
</intent-filter>
</service>

Can you direct me for a solution?
 
It looks like you are trying to start an activity from a service using an Intent. In order to start an activity from a service, you will need to add the FLAG_ACTIVITY_NEW_TASK flag to the Intent. This flag tells Android to create a new task for the activity being launched, as activities can only be launched from within the context of a task.

Here is how you can modify your code to add the FLAG_ACTIVITY_NEW_TASK flag:

Code:
Intent i = new Intent(getApplicationContext(), PdfWiever.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// ... other code goes here
startActivity(i);

Additionally, make sure that you have declared the PdfWiever activity in your app's AndroidManifest.xml file and that it has the appropriate intent-filter elements to handle the types of data that you want to pass to it.

It's also a good idea to check the logcat output for any error messages that may be relevant to this issue. This can help you identify any issues with the activity or the Intent itself.
 
  • Like
Reactions: ocnbrze
Upvote 0
Grazie per la risposta. Hai ragione, oltre al codice, che ho anche intuito, sai dov'era il problema? Solo nel filtro intent in particolare nel tag type. Grazie lo stesso, ottima risposta. Buon 2023!!!!
this is an english only site.

use Google Translate
Thanks for the reply. You are right, besides the code, which I also guessed, do you know where the problem was? Only in the intent filter specifically in the type tag. Thanks all the same, great answer. Happy 2023!!!!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones