I try to play with Service in Android, my application contain an Activity with a button so that it can do action when I press it.
The core component is the ExtractingURLService , which extends IntentService framework class.
I have overrided the onHandleIntent() method, so that it can handle intent passed to it properly.
Code:
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService(String name) {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
}
The Android Manifest file is :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.service.urlextraction"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ServiceExample02Activity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExtractingURLService" android:enabled="true"></service>
</application>
</manifest>
In my only Activity class, I call the service by using:
Intent startServiceIntent = new Intent(this, ExtractingURLService.class);
startService(startServiceIntent);
That's all my work, but when deploy, the runtime error I received was:
04-09 16:24:05.751: ERROR/AndroidRuntime(1078): Caused by:
java.lang.InstantiationException:
example.service.urlextraction.ExtractingURLService
What I should do with that ??
Add full path of Service
<service android:name="com.foo.services.ExtractingURLService" android:enabled="true"></service>
Intent Service should look like this:
package com.foo.services;
import android.app.IntentService;
import android.content.Intent;
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService() {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
You can add try and catch block to avoid crash.Its work for me
try {
Intent intent = new Intent(DashboardActivity.this, MyService.class);
startService(intent);
}catch (IllegalStateException e){
}catch (Exception ee){
}
Related
This is little snipet to try BroadcastReciever android but i dont understand wath's wrong.
In fact just callIntent return log. my little brain it's burning.
if somebody can tell me why ?
Sorry for my bad english !
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.plectre.broadcast">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReciever" >
<intent-filter>
<action android:name="com.plectre"/>
</intent-filter>
</receiver>
</application>
</manifest>
```
public void callIntent() {
Intent intent = new Intent();
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
```
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
```
No error, no crash, nothing !! :=(
Create an explicit intent by explicitly declaring the receiver when you create the intent. At present your intent is implicit.
public void callIntent() {
// Explicitly declare the receiver class
Intent intent = new Intent(this, MyReciever.class);
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
Additional reading: https://developer.android.com/guide/components/intents-filters#ExampleExplicit
Also. You declare the intent's Extra as "frist" and try to receive at as "first". This needs to be rectified.
You should ensure that the correct action is taken with the specified Intent Action in your receiver:
#Override
public void onReceive(Context context, Intent intent) {
if ("com.plectre".equals(intent.getAction())) {
final String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
}
I'm studying Android.
I try to implement a Custom static Broadcast Receiver but it is not working.
I search for some issue from Google but I can't find something to solve this.
I work on Android 7.0 Min Level 24 Target Level 28
In fact, MyStaticReceiver isn't launching when I start the Activity (no log)
MyDynamicReceiver work perfectly
Do you have a solution?
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.receiver">
<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="test.receiver.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyStaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="#string/StaticAction" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java :
public class MainActivity extends Activity {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
private MyDynamicReceiver dynamicReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName receiver = new ComponentName(this, MyStaticReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
#Override
protected void onResume() {
super.onResume();
if (Debug) Log.i(TAG, "MainActivity:onResume");
configureDynamicReceiver();
}
#Override
protected void onDestroy(){
super.onDestroy();
if (Debug) Log.i(TAG, "MainActivity:onDestroy");
unregisterReceiver(dynamicReceiver);
}
public void onClickButton(View v) {
if (Debug) Log.i(TAG, "MainActivity:onClickButton");
Intent staticIntent = new Intent();
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
Intent dynamicIntent = new Intent();
dynamicIntent.setAction(getString(R.string.DynamicAction));
sendBroadcast(dynamicIntent);
}
public void configureDynamicReceiver() {
if( dynamicReceiver == null ) {
dynamicReceiver = new MyDynamicReceiver();
}
IntentFilter filter = new IntentFilter(getString(R.string.DynamicAction));
registerReceiver(dynamicReceiver, filter);
}
}
MyStaticReceiver.java (MyDynamicReceiver is the same ...)
public class MyStaticReceiver extends BroadcastReceiver {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
public MyStaticReceiver() {
if (Debug) Log.i(TAG, "MyStaticReceiver");
}
#Override
public void onReceive(Context context, Intent intent) {
if (Debug) Log.i(TAG, "MyStaticReceiver:onReceive");
}
}
You are actually sending an implicit broadcast, therefore the receiver declared in the manifest will not work.
If your app targets Android 8.0 (API level 26) or higher, you cannot use the manifest
to declare a receiver for most implicit broadcasts (broadcasts that
don't target your app specifically). You can still use a
context-registered receiver when the user is actively using your app. Link
You don't face any issue with MyDynamicReceiver because it is context-registered receiver.
But to make it work for MyStaticReceiver, you can try sending an explicit broadcast by passing the component name in the constructor of the Intent.
Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
I've the same issue described here but i can't understand whats wrong.
My issue is: Unable to start service Intent { act=.connections.MoodyService } U=0: not found
EDIT
I've changed my package from connections to service in the source code, sorry for the confusion
My manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moody"
android:installLocation="auto"
android:versionCode="0"
android:versionName="0.6.7 alpha" >
<uses-sdk
android:maxSdkVersion="18"
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="activities.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="activities.Menu_esq"
android:label="#string/title_activity_menu_esq" >
</activity>
<activity
android:name="activities.BaseActivity"
android:label="#string/title_activity_base" >
</activity>
<activity
android:name="activities.MainView"
android:label="#string/title_activity_main_view" >
</activity>
<activity
android:name="activities.LoginActivity"
android:label="#string/app_name"
android:noHistory="true"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.moody.LeftActivity"
android:label="#string/title_activity_left" >
</activity>
<activity
android:name="com.example.moody.RightActivity"
android:label="#string/title_activity_right" >
</activity>
<activity
android:name="activities.UserDetailsActivity"
android:label="#string/title_activity_user_details" >
</activity>
<activity
android:name="fragments.TopicsPreview"
android:label="#string/title_activity_copy_of_topics_preview" >
</activity>
<activity android:name="activities.Loading" >
</activity>
<service
android:name=".service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
</service>
</application>
service is the package and MoodyService is the class name
My service class
public class MoodyService extends Service {
public MoodyService() {
// TODO Auto-generated constructor stub
}
private boolean isRunning = false;
Object getContent;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// Announcement about starting
Toast.makeText(this, "Starting the Demo Service", Toast.LENGTH_SHORT)
.show();
// Start a Background thread
isRunning = true;
Thread backgroundThread = new Thread(new BackgroundThread());
backgroundThread.start();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
// Stop the Background thread
isRunning = false;
// Announcement about stopping
Toast.makeText(this, "Stopping the Demo Service", Toast.LENGTH_SHORT)
.show();
}
private class BackgroundThread implements Runnable {
int counter = 0;
public void run() {
try {
counter = 0;
while (isRunning) {
System.out.println("" + counter++);
new Contents().getAll(getResources(),
getApplicationContext());
Thread.currentThread().sleep(5000);
}
System.out.println("Background Thread is finished.........");
} catch (Exception e) {
e.printStackTrace();
}
}
}
And in my main Intent.
Intent start = new Intent(".service.MoodyService");
this.startService(start);
and also tried
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
and tried with the full path
<service
android:name="com.example.moody.service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
Solved
I deleted the period in the beginning of the package name in the manifest and it worked, in another words:
This doesn't work:
.yourPackage.YourClass
But this does work:
yourPackage.YourClass
And in the main:
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
But it goes against what is written in the documentation:
android:name The name of the Service subclass that implements the service. This should be a fully qualified class name (such as,
"com.example.project.RoomService"). However, as a shorthand, if the
first character of the name is a period (for example, ".RoomService"),
it is appended to the package name specified in the
element. Once you publish your application, you should not change this
name (unless you've set android:exported="false").
There is no default. The name must be specified.
The service must be also be included in the Manifest:
<service android:name="com.x.x.serviceclass"></service>
Make sure you have declared your service in the manifest file.
<service
android:name=".MyService"
android:enabled="true" >
</service>
and try writing getApplicationContext() instead of "this" keyword
startService(new Intent(getApplicationContext(), MoodyService.class));
I don't know why you are using that package-like name for your service name, but why don't you use class name for starting the service?
Intent intent = new Intent(context, YourService.class);
context.startService(intent);
I think in manifest package name for service is wrong as you said your package name is connections so it should be like this
android:name ="connections.MoodyService"
or
android:name="com.example.moody.connections.MoodyService"
to invoke service do
Intent intent = new Intent(this, MoodyService.class);
this.startService(intent);
my service is in "service" package and my manifest service enrty like this;
<service
android:name="service.TimerService"
android:exported="false" >
</service>
Did you create an empty constructor in the service?
If not, try that.
Also uncertain if you can use the Intent like that. You could try the 'new Inten(this, MoodyService.class)' construction.
Did you try to use the android:name that you specified in the Manifest?
Android Manifest:
<service
android:enabled="true"
android:name="UploadService" />
The call would be sth like this:
Intent intent = new Intent("UploadService");
My English is poor. I cannot start an android service in a boot time and I do not know the problem. I was trying example codes, but without success. Can somebody send me a project in Java that runs? Other code works for other people but on my tablet smartphone emulator it does not work. Does a problem exist in android 4.0?
This is my code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service22"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
>
<supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<service android:name="com.example.MyService">
<intent-filter>
<action android:name="com.example.MyService">
</action>
</intent-filter>
</service>
<receiver android:name="com.example.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
public class MyService extends Service
{
private static final String LOG_TAG = "::Monitor";
#Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
for (int i = 0 ; i < 20 ; i++)
{
mensaje();
}
Log.e(LOG_TAG, "Service started.");
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
#Override
public IBinder onBind(Intent intent)
{
Log.e(LOG_TAG, "Service bind.");
return null;
}
public void mensaje()
{
Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show();
}
}
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
}
String LOG_TAG = "::StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("OnBootReceiver", "Hi, Mom!");
}
}
and manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/cw"
android:label="#string/app_name">
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I think:
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
is not enough,you must set intent class name like that:
Intent i = new Intent();
i.setClassName("com.example", "com.example.MyService");
i.setAction("com.example.MyService");
context.startService(i);
pls try that.
IMPORTANT: Android OS 3.1+ remains ignorent about your broadcast receivers in following cases:
1.User has never started the application explicitly at least once.
2.User has "force closed" the application.
Issue has nothing to do to your implementation. This is a potential security hole in Android that Google has closed :)
I am currently trying to make a broadcast receiver which will invoke after android device boots and then will run a background service. I have tried many examples but don't know where I'm going wrong. I am following this example:
https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot
I have imported this whole project in my workspace and tried to run. But the receiver didn't invoked or so.
Please help me out.
My Testing Device is: Motorolla Xoom with ICS 4.0.3
EDIT
Manifest
<uses-sdk android:minSdkVersion="8" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REBOOT" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service
android:name="awais.soft.MyService"
android:enabled="true" >
<intent-filter>
<action android:name="awais.soft.MyService" >
</action>
</intent-filter>
</service>
<receiver android:name="awais.soft.ServicesDemoActivity" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
</application>
Broadcast Receiver
package awais.soft;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class ServicesDemoActivity extends BroadcastReceiver {
static final int idBut = Menu.FIRST + 1, idIntentID = Menu.FIRST + 2;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Awais", "onReceive:");
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Intent i = new Intent();
i.setAction("awais.kpsoft.MyService");
context.startService(i);
}
}
}
Service
package awais.soft;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.is);
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
I am something like this in My app and Its Working for me.
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public final void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// CustomLog.i("Boot Completed");
}
}
}
Android Manifset
<receiver android:name=".model.service.DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.HOME"></category>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.REBOOT" />
Please check if you have given permission for RECEIVE_BOOT_COMPLETED
see i am posting you eample that will help you
For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events).
At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.
Step 1: First you'll need to create a simple service, defined in Monitor.java:
public class Monitor extends Service {
private static final String LOG_TAG = "::Monitor";
#Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(LOG_TAG, "Service started.");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
#Override
public IBinder onBind(Intent intent) {
Log.e(LOG_TAG, "Service bind.");
return null;
}
}
Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
private static final String LOG_TAG=StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.package.Monitor");
context.startService(i);
}
}
}
Step 3: Finally, your AndroidManifest.xml file must contain the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.package.Monitor"
android:versionName="1.0"
android:versionCode="100"
android:installLocation="internalOnly">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="test.package.Monitor">**
<intent-filter>
<action android:name="test.package.Monitor">
</action>
</intent-filter>
</service>
<receiver android:name="test.package.StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest
Everything was fine..:S
The problem was with device..(i.e. Motorolla Zoom ICS 4.0.3)
Now tested on Galaxy Tab With 2.2 and Working fine..
Thanks all for your time
If your phone is rooted then you will have trouble in Android Boot-Up BroadCast invoking otherwise you have to ensure your app has required root permissions
The problem persists in the case of devices having android version more than 3.0, by the way its not the problem it has been done for security purposes by google i guess..If u have to run the service on boot you have to make a custom intent & broadcast it. For making custom intent you have to make a service file from where u have to broadcast that intent on boot complete & your service file(that u want to run) will receive that intent on its onReceive method & your service will run.One more thing the service file you will create to call your service that you want to run should be kept on system/app folder of file explorer of device, if your file system shows sorry read only file system then from command prompt do just adb remount & then push the file on device,restart your system your service will run..Cheers!!