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.
Related
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.
I have the following code in a service which is called with an AlarmManager (this works well) but when when I start the wifi scan (which returns true) I never receive the results in the BroadcastReceiver I created:
public class WifiCheckerService extends IntentService {
WifiManager wifiManager;
List<Wifi> savedWifis;
List<Wifi> wifisInRange;
List<ScanResult> wifisInRangeResults;
BroadcastReceiver broadcastReceiver;
public WifiCheckerService() {
super("WifiCheckerService");
}
#Override
public void onCreate() {
super.onCreate();
setScanAlwaysAvailable();
if(broadcastReceiver == null)
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
check();
}
};
if (wifiManager == null)
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(broadcastReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
#Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
wifiManager.startScan();
}
}
private void check() {
// irrelevant stuff
Intent intent = new Intent(Constants.BROADCAST_UPDATE_WIFI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendBroadcast(intent);
}
}
public List<Wifi> getSavedWifisInRange(List<Wifi> savedWifis,
List<ScanResult> wifisInRangeResults) {
List<Wifi> wifisInRange = new ArrayList<Wifi>();
for (Wifi wifi : savedWifis) {
for (int i = 0; i < wifisInRangeResults.size(); i++) {
if (wifi.getSsid().equals(wifisInRangeResults.get(i).SSID)) {
wifi.setRssi(String.valueOf(wifisInRangeResults.get(i).level));
wifisInRange.add(wifi);
}
}
}
return wifisInRange;
}
public void setScanAlwaysAvailable() {
/* Set scan always available if it is turned off*/
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (!wifiManager.isScanAlwaysAvailable()) {
startActivity(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE));
}
}
}
public boolean isTetheringActive() {
//irrelevant stuff
return false;
}
private boolean isAnySavedWifiInRange() {
//irrelevant stuff
return true;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(wifiScanReceiver);
}
}
I have used BroadcastReceiver's with wifi scans before, but I don't know why this is not working.
What's wrong?
EDIT: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name=".WifiSentinelApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.DonateActivity"
android:label="#string/donate"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".activities.SettingsActivity"
android:label="#string/settings"
android:screenOrientation="portrait" />
<receiver android:name=".receivers.AlarmReceiver" />
<receiver android:name=".receivers.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".services.WifiCheckerService" />
</application>
My understanding of the IntentService is that it will only be active for the time needed to handle the request (ie, the time needed to run onHandleIntent()). I haven't tested your code, but my guess is that your broadcast receiver is being unregistered right away when onDestroy() runs, before the scan results are delivered. Probably an IntentService is not right for this. You might need to create a "regular" (long living background) service. Or, maybe it just makes more sense to have your BroadcastReceiver in the activity where you will show the scan results and get rid of this IntentService altogether. WifiManager::startScan() is asynchronous so it shouldn't be necessary to call that on a background service anyway.
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
I am Creating incoming call Receive activity by SipDemo . My activity has two button "accept call" and "reject call". When i got incoming call then my own activity is open such as
----> incoming call class ----> onReceive()----> go to my call pick activity.
here is this above two button such as accept and reject call. but when i hit on "accept call" . A Nullpointer Exception generate. it is on SipAudiocall incoming;
this is incoming class:-
static SipAudioCall incomingCall = null;
#Override
public void onReceive(final Context context, Intent intent) {
Intent startActivity = new Intent();
startActivity.setClass(context, Mycall.class);
startActivity.setAction(IncomingCallReceiver.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(startActivity);
}
public static void acceptCall() {
incomingCall.sendDtmf(9);
try {
incomingCall.sendDtmf(9);
incomingCall.answerCall(200);
//wtActivity.gototimer("i");
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// wtActivity.gototimer("i");
}
public void rejectCaLL() {
try {
incomingCall.endCall();
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is my call pick activity class:-
public class Mycall extends Activity {
Button acct;
IncomingCallReceiver incomingCallReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
// Note that none of the preferences are actually defined here.
// They're all in the XML file res/xml/preferences.xml.
super.onCreate(savedInstanceState);
setContentView(R.layout.my);
acct = (Button) findViewById(R.id.button1);
acct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IncomingCallReceiver.acceptCall();
}
});
}
}
This is AndroidManifest.xml :-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.sip">
<application android:icon="#drawable/icon" android:label="SipDemo">
<service android:name=".MService">
</service>
<activity android:name=".WalkieTalkieActivity"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SipSettings" android:label="set_preferences"/>
<receiver android:name=".IncomingCallReceiver" android:enabled="true" android:label="Call Receiver">
<intent-filter>
<action android:name="android.SipDemo.INCOMING_CALL" />
</intent-filter>
</receiver>
<activity android:name=".Mycall"
>
</activity>
<activity android:name=".Myclass"/>
</application>
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.USE_SIP" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-feature android:name="android.hardware.sip.voip" android:required="true" />
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-feature android:name="android.hardware.microphone" android:required="true" />
Please Expert Help me. I has involved this problem since 1 month.
I am wait for your response.
Thank you
Becouse your
IncomingCallReceiver incomingCallReceiver;
is null at time of calling it...
If you create a static class you must take care to initiate it maybe in creator-method.
create an abstract class:
public abstract class IncomingCallReceiver
and change your method to public static final
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);