trigger a receiver if system date change automatically in android - android

I am a newbie for android. I have registered receiver in manifest using android.intent.action.DATE_CHANGED and it is working fine when a user changes the date manually. BUT my requirement is receiver has to be called automatically everyday the system date change say at 12:00 am. The user should not change the date.
Thanks in advance.
my code - manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
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=".NotificationOne"
android:label="#string/title_activity_notification_one"
android:parentActivityName=".FirstActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".FirstActivity" />
</activity>
<service android:name=".MyService">
</service>
<receiver android:name=".MyBroadcastreceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".SecondBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
Broadcast Receiver class
public class SecondBroadcastReceiver extends BroadcastReceiver {
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
Toast.makeText(context,"inside second",Toast.LENGTH_SHORT).show();
MyBroadcastreceiver.firstConnect = true;
MyBroadcastreceiver.hasInternet = isInternetAvailable();
if(MyBroadcastreceiver.hasInternet) {
context.startService(new Intent(context, MyService.class));
MyBroadcastreceiver.firstConnect = false;
}
}
public boolean isInternetAvailable() {
Toast.makeText(mContext,"inside isInternetAvailable",Toast.LENGTH_SHORT).show();
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
one service- which has notification
public class MyService extends Service {
private NotificationManager myNotificationManager;
private int notificationIdOne = 111;
private int numMessagesOne = 0;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
displayNotificationOne();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
i have another receiver which will be called when internet is connected/disconnected.

Related

Android self startup

I wrote a application,I wrote an application. I want it to start a service to connect to the WebSocket server, and then send a notification at an appropriate time,I tried to write a piece of code, but the server log has no connection information,This is my
code:
In mainifests.xml:
<!--permission-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!--receiver -->
<receiver android:name=".service.StartNotificationReceiver"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
<!--service-->
<service android:name=".service.NotificationService"/>
In NotificationService.java:
package com.xxxx.xxxx.service;
public class NotificationService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
}
private void createChannel(){
}
private void sendNotification(String title,String content){
}
}
In StartNotificationReceiver.java
package com.ecsoft.zyymaintain.service;
public class StartNotificationReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
Log.e(“sot”,”onReceive:"+intent.getAction());
if (ACTION.equals(intent.getAction())){
Intent intentService = new Intent(context, NotificationService.class);
context.startService(intentService);
}
}
}
This is my core code,and my application is cant start service when OS startup
I use below code can start the service
<service
android:name=".AutoStartService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".AutoStartReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

How to make an application start on factory reset/first startup?

I am looking to have some basic forms run on my android device after it is reset or on the first boot like most smartphones ask you to connect to Wi-Fi, register the device etc. I haven't worked with native android development previously so what are my options and what areas should I look into?
Try this
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
}
MyService.java
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), MyService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".MyService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Unable to create service

I'm new to android and creating a service in one of my reminder application. but I my service is triggering when i call the startService() in my onCreate() method of my activity I am unable to see the toasts which are used in different methods of my service class.As new to android unable to figure out the problem.
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_show_reminders);
isReminders = false;
dataSrc = new RemitDataSrc(this);
dataSrc.open();
rem = dataSrc.findALL();
refreshDisplay();
}
private void refreshDisplay(){
if(isMyServiceRunning(GeofencingService.class)){
Intent intent = new Intent(this, GeofencingService.class);
stopService(intent);
Toast.makeText(this, "yes service", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "No service", Toast.LENGTH_LONG).show();
}
if(rem.size()==0){
Toast.makeText(this, "No Reminders in Database", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent(this, GeofencingService.class);
startService(intent);
ArrayAdapter<Reminders> remAdpt = new RemindersListAdapter(this, rem);
setListAdapter(remAdpt);
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
GeofencingService:
public class GeofencingService extends Service{
RemitDataSrc dataSrc;
/**
* Geofence Store
*/
private GeofenceStore mGeofenceStore;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "GeoFencing Service created", Toast.LENGTH_LONG).show();
dataSrc = new RemitDataSrc(this);
dataSrc.open();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "GeoFencing Service Started", Toast.LENGTH_LONG).show();
serviceTest sT= new serviceTest();
dataSrc.open();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "GeoFencing Service Stoped", Toast.LENGTH_LONG).show();
dataSrc.close();
}
}
manifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SetReminderActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD5GMap3cZBiMYWEiK0WX-2whx0xuqBwW4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".AddReminderActivity"
android:label="#string/title_activity_add_reminder" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityShowReminders"
android:label="#string/title_activity_activity_show_reminders" >
</activity>
<activity
android:name=".RemindersDetailActivity"
android:label="Reminder Details" >
</activity>
<service
android:name=".LocationCheckerService"
>
</service>
<service
android:name=".GeofencingService"
android:exported="false"
>
</service>
</application>
As you have mentioned in one of one of your comments that the package name of service is different from the package containing activity, use the fully qualified class name in manifest with package name.
<service
android:name="com.afifa.projectreminder.service.GeofencingService" android:exported="false"
>
</service>
Please register your service to your manifest as a children of your application like:
<application ..>
..
<service
android:name=".GeofencingService"
android:enabled="true">
</service>
</application>
Mention the service in the manifest of your project under application Tag.
like so
<service android:name=".ServiceName" />

Lock Screen Volume Control with CCL and without CCL

I can listen volume level in my VolumeService. This service listen to volume level in background.
My problem is: I can't control volume in lock screen.
I tried two different ways for volume control
First Way with CCL
Application Class:
public class CastApplication extends Application {
private static String APPLICATION_ID;
public static final double VOLUME_INCREMENT = 0.03;
#Override
public void onCreate() {
super.onCreate();
APPLICATION_ID = getString(R.string.app_id);
// initialize VideoCastManager
VideoCastManager.initialize(this, APPLICATION_ID, VideoCastControllerActivity.class, null).
setVolumeStep(VOLUME_INCREMENT).enableFeatures(VideoCastManager.FEATURE_NOTIFICATION |VideoCastManager.FEATURE_LOCKSCREEN |
VideoCastManager.FEATURE_WIFI_RECONNECT | VideoCastManager.FEATURE_CAPTIONS_PREFERENCE |VideoCastManager.FEATURE_DEBUGGING);
}
}
MainActivity:
public class MainActivity extends FragmentActivity {
private VideoCastManager mCastManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoCastManager.checkGooglePlayServices(this);
mCastManager = VideoCastManager.getInstance();
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mCastManager.onDispatchVolumeKeyEvent(event, CastApplication.VOLUME_INCREMENT)) {
return true;
}
return super.dispatchKeyEvent(event);
}
}
Second way without CCL (with RRC)
MainActivity:
public class MainActivity extends FragmentActivity {
ContentObserver mSettingsContentObserver;
RemoteControlClient remoteControlClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_RING,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
RemoteControlClientCompat mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
remoteControlClient = new RemoteControlClient((PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0)));
MediaRouter.getInstance(this.getApplicationContext()).addRemoteControlClient(remoteControlClient);
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
}
Service and ContentObserver are same for both way
VolumeService:
public class VolumeService extends Service {
ContentObserver mSettingsContentObserver;
#Override
public void onCreate() {
super.onCreate();
mSettingsContentObserver = new SettingsContentObserver(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,mSettingsContentObserver);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}
}
ContentObserver:
public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;
public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context = c;
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int delta = previousVolume - currentVolume;
if (delta > 0) {
Log.i("MyService", "Volume is down");
previousVolume = currentVolume;
} else if (delta < 0) {
Log.i("MyService", "Volume is up");
previousVolume = currentVolume;
}
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:name="com.yns.volumelock.CastApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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>
<receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
</intent-filter>
</service>
<service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService" />
<activity
android:name="com.google.android.libraries.cast.companionlibrary.cast.tracks.CaptionsPreferenceActivity"
android:label="#string/action_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service
android:name="com.yns.volumelock.VolumeService"
android:enabled="true" >
</service>
</application>

How to start service at boot on android 4.0.3

I wan't to start service at boot on android 4.0.3 but when i boot my device it not happen in my device and service not start.Help me please thank you
here is my manifest
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<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>
<service android:name="com.android.testsharedpreference.MyService"></service>
<receiver android:name="com.android.testsharedpreference.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
my boardcast receiver
public class BootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
context.startService(new Intent(context,MyService.class));
}
}
}
and my service
public class MyService extends Service
{
#Override
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
super.onCreate();
Toast.makeText(MyService.this, "onCreate Service ", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(MyService.this, "onDestroy Service ", Toast.LENGTH_LONG).show();
}
}
you should add add android.permission.RECEIVE_BOOT_COMPLETED as it was not required before ICS so the app was working fine but starting from ICS is you don't have this permission your app wont receive the boot completed intent
Don't forget to override the method "onStartCommand" in your MyService:
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String action = intent == null ? null : intent.getAction();
Log.d("onStartCommand", "action = " + action);
/* ... */
return START_NOT_STICKY; // for example ...
}

Categories

Resources