Android Intent causes NoClassDefFoundError - android

I have encountered the NoClassDefFoundError when trying to create a new Intent to start a Service. I have checked several suggested solutions such as including libraries in the /libs path, I believe that I have these set up correctly. I have included my manifest and source below. Any help would be really appreciated.
Here is a screen capture of my file structure inside Eclipse
Here is my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ajinex.easysave"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name=".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>
<activity
android:name=".CouponsActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:label="#string/title_activity_coupons" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- service -->
<service android:enabled="true" android:name=".LocationCheckService" >
</service>
</application>
Here is my calling code from the MainActivity:
Intent LocationCheck = new Intent(this, LocationCheckService.class);
this.startService(LocationCheck);
Here is my LocationCheckService.class
public class LocationCheckService extends Service {
public class LocalBinder extends Binder {
LocationCheckService getService() {
return LocationCheckService.this;
}
}
public final IBinder mBinder = new LocalBinder();
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(checkNetworkRechability()){
Toast.makeText(this, "EasySave Service Started", Toast.LENGTH_LONG).show();
getSysLocation();
}
return START_STICKY;
}
#Override
public void onCreate() {
}
#Override
public void onDestroy() {
}
}
Here is some LogCat output relating to the issue:
07-16 00:09:36.712: I/AndroidRuntime(312): NOTE: attach of thread 'Binder Thread #3' failed
07-16 00:09:37.702: W/ActivityManager(59): Unable to start service Intent { act=com.ajinex.easysave.location.LocationCheckService }: not found
07-16 00:09:38.162: I/ActivityManager(59): Displayed activity com.ajinex.easysave/.MainActivity: 1568 ms (total 1568 ms)

Based on the error message (com.ajinex.easysave.location.LocationCheckService) and the package name (com.ajinex.easysave) you need to declare the service as .location.LocationCheckService in the manifest file.

try as:
Intent LocationCheck = new Intent();
LocationCheck.setComponent(new ComponentName("com.ajinex.easysave",
"com.ajinex.easysave.LocationCheckService"));
this.startService(LocationCheck);

Related

PackageBroadcastReceiver not working after app is closed

I have a PackageBroadcastReceiver running ok while the app is in the background. I need it to keep listening to packages added/removed when it is closed. But, it is not.
Is there any way to do this with a BroadcastReceiver or should I move to a service?
Here the code:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cresan.antivirus.StockingActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.tech.applications.coretools.advertising.PackageBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
And my PackageBroadcastReceiver:
public class PackageBroadcastReceiver extends BroadcastReceiver
{
static IPackageChangesListener _listener;
static public void setPackageBroadcastListener(IPackageChangesListener listener)
{
_listener=listener;
}
#Override
public void onReceive(Context ctx, Intent intent)
{
if(_listener!=null && Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()))
_listener.OnPackageAdded(intent);
if(_listener!=null && Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()))
_listener.OnPackageRemoved(intent);
}
}
Cheers.
You will definitely need to implement a background service to do this. See the developer guide.
You can then programmatically register a BroadcastReceiver in your service:
private PackageBroadcastReceiver packageBroadcastReceiver;
#Override
public void onCreate() {
super.onCreate();
this.packageBroadcastReceiver = new PackageBroadcastReceiver();
this.packageBroadcastReceiver.setPackageBroadcastListener(new IPackageChangesListener() {
public void OnPackageAdded(Intent i) {
//do something here- probably start an activity
}
...
});
IntentFilter packageFilter = new IntentFilter("android.intent.action.PACKAGE_ADDED");
packageFilter.addAction("android.intent.action.PACKAGE_INSTALL");
packageFilter.addDataScheme("package");
this.registerReceiver(this.packageBroadcastReceiver, packageFilter);
}
#Override
public void onDestroy(){
super.onDestroy();
this.unregisterReceiver(this.packageBroadcastReceiver);
}
Try registering broadcast receiver in service. After activity is destroyed, receiver will be destroyed to so you will not get broadcast event.

Autostart app immediately after the boot

I want to start my application when phone startup
I just follow tutorial from here but it doesn't work in my device. Please see my method:
package net.londatiga.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startService(startServiceIntent);
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.londatiga.android"
android:versionCode="2" android:versionName="1.01">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="net.londatiga.android.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".ExampleActivity"
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>
Where is my mistake please?
Instead of:
context.startService(startServiceIntent);
Use:
context.startActivity(startServiceIntent);
You don't have any Service, You need to open activity.
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startActivity(startServiceIntent);
Create a class name it as AfterBootActivity:
public class AfterBootActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Now, Create a new class named as Autostart.java which extends a BroadcastReceiver:
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, StarterService.class);
context.startService(intent);
}}
In the Manifest file add this class as a receiver. This class will listen to the Broadcast call the Android OS sends after the boot sequence has finished i.e. after the phone started up.
Now Create a class named as StarterService.java which will extend Service:
public class StarterService extends Service {
private static final String TAG = "MyService";
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The below started service opens the Activity.
*/
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}}
When the class Autostart receives the BOOT_COMPLETED Broadcast from Android OS it will start the StarterService which then starts the Android Activity “AfterBootActivity” i.e our main class. We can play any audio/video or anything in it.
Change your Manifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="on.boot.completed"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="on.boot.completed.AfterBootActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="on.boot.completed.Autostart" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="on.boot.completed.StarterService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Also Remember to install it in internal memory because if the app installed on the SD Card then autostart will not work! That’s why it’s important that we add in manifest.
android:installLocation="internalOnly"
That’s all run your app.
After it has been started turn off your phone and turn it back on and the app would start automatically after the device has booted up.

Security Exception: Not allowed to start a service Intent

Ok i am new to android development so please bear with me here. I am following the learning android book and have this problem where the refresh service wont work as it doesnt have the necessary permission. Can anyone tell me what is causing the issue? The logcat throws up runtime error: *java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example.yamba/.RefreshService } without permission com.example.yamba.permission.REFRESH at com.example.yamba.TimelineActivity.onOptionsItemSelected(TimelineActivity.java:138)
*
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yamba"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.example.yamba.permission.REFRESH" />
<application
android:name=".YambaApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.yamba.StatusActivity"
android:configChanges="orientation"
android:icon="#drawable/ic_launcher"
android:label="#string/status_Update" >
</activity>
<activity
android:name=".TimelineActivity"
android:icon="#drawable/ic_launcher"
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=".UpdaterService" >
</service>
<service
android:name=".RefreshService"
android:permission="com.example.yamba.permission.REFRESH" >
<intent-filter>
<action android:name="com.example.yamba.RefreshService" />
</intent-filter>
</service>
<activity
android:name=".PrefsActivity"
android:label="#string/Preferences" >
</activity>
<receiver android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.example.yamba.REFRESH_ALARM" />
</intent-filter>
</receiver>
</application>
package com.example.yamba;
import java.util.List;
import winterwell.jtwitter.Twitter.Status;
import winterwell.jtwitter.TwitterException;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class RefreshService extends IntentService {
static final String TAG = "RefreshService";
public RefreshService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
((YambaApp) getApplication()).pullAndInsert();
Log.d(TAG, "onHandleIntent");
}
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "OnCreated");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "OnDestroy");
}
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Intent intentUpdater = new Intent(this, UpdaterService.class);
Intent intentRefresh = new Intent(this, RefreshService.class);
Intent intentPrefs = new Intent(this, PrefsActivity.class);
Intent intentTimeline = new Intent(this, StatusActivity.class);
switch (item.getItemId()) {
case R.id.item_start_service:
startService(intentUpdater);
return true;
case R.id.item_stop_service:
stopService(intentUpdater);
return true;
case R.id.item_refresh:
startService(intentRefresh);
return true;
case R.id.item_prefs:
startActivity(intentPrefs);
return true;
case R.id.item_status_update:
startActivity(intentTimeline);
default:
return false;
}
The onOptionsItemSelected is used to call the refresh service.
Really Appreciate the help. Thanks!!
you need to declare permission in your manifest
<permission
android:name="com.example.yamba.permission.REFRESH"
android:protectionLevel="signature"
/>
You need to declare this REFRESH Permission in the manifest as well.
here is an example code of how to declare a permission
<permission
android:name="A_PERMISSION"
android:description="#string/broadcast_permission_desc"
android:label="#string/broadcast_permission_label"
android:permissionGroup="#string/broadcast_permission_group"
android:protectionLevel="signature" />
For Reference See this Page

Not allowed to start service Intent

I'm trying to call a service from a activity:
When I'm running the program the activity throws an error which says: Not allowed to start service Intent. What am I doing wrong? I'm sorry for possible stupid mistakes in the code, but I'm a newbie.
activity code:
public void startService() {
try {
startService (new Intent ( this , SmsReminderService.class)) ; }
catch (Exception e ) { Log.v("Error" , e.getMessage()) }
}
service code :
public class SmsReminderService extends Service {
#Override
public void onStart(Intent intent, int startid) {
Log.v("SSms", "Service started") ; }}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.sms.smsReminder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<permission android:name="SEND_SMS"></permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".SmsReminderActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".SmsReminderService"
android:permission="android.permission.BIND_REMOTEVIEWS">
<intent-filter>
<action android:name = "android.intent.category.LAUNCHER" ></action>
</intent-filter>
</service>
</application>
</manifest>
Thanks in advance, Tom
Why this in the service?
<intent-filter>
<action android:name = "android.intent.category.LAUNCHER" ></action>
</intent-filter>
Try to add this:
<uses-permission android:name="android.permission.BIND_REMOTEVIEWS" />

Run a task in background always in android

I have a problem that I made a service which executes a task in background after every 5 seconds, for this I made a receiver in manifest and autostarter class but it is not excuted in my app. Means the service is not running in the app. I don't know why? Please suggest me for the right answer.
Android Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="com.halosys.TvAnyTime.ServiceAutoStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainScreen"
android:label="#string/app_name" android:screenOrientation="portrait" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntroVideo" android:label="#string/app_name" android:theme="#style/CustomTheme" android:screenOrientation="landscape"/>
<activity android:name=".WelcomeScreen1" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/>
<activity android:name=".IntroFaceBookScreen" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/>
</application>
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.READ_INPUT_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
ServiceAutoStarter:
public class ServiceAutoStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, ServiceTemplate.class);
myIntent.putExtra("extraData", "somedata");
context.startService(myIntent);
}
}
ServiceTemplate.java:
public class ServiceTemplate extends Service {
Runnable refresher;
public static int HashesInPattern=32; // 32 64byte hashes in the pattern
public static int BytesInHash =64;
static byte[] hashPattern;
ArrayList<byte[]> finalHashafterXor = new ArrayList<byte[]>();
byte[] finaldatafile = new byte[2097152];
byte[] finalhash;
InputStream is;
byte[] bytesafterXor,bytes;
String strLine;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//code to execute when the service is first created
}
#Override
public void onDestroy() {
//code to execute when the service is shutting down
}
#Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
final Handler handler = new Handler();
refresher = new Runnable() {
public void run() {
// some action
try{
Encrypt();
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
handler.postDelayed(refresher, 2000);
}
};
}
Thanks in advance.
I haven't gone through whole of your code but i saw manifest file
Like activities (and other components), you must declare all services in your application's manifest file.
Please do change and try.

Categories

Resources