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>
Related
I am working on an application for collecting crash reports in application, so that I have created class which extends Application for handling uncaught exception and created service class for communicate with server.
I made a simple divide by zero exception in a button click and it produce exception but my service class does not called. I am sure have registered the service class in manifest file.
my question is Will Service concept work even if application is crashed (force close) in Android?
code:
public class MyApp extends Application {
public void onCreate() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable e) {
handleUncaughtException(thread, e);
}
});
}
public void handleUncaughtException(Thread thread, Throwable e) {
e.printStackTrace();
Intent i = new Intent(MyApp.this, ServiceClass.class);
startService(i);
}
}
Service class:
public class ServiceClass extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("onStartCommand", "onStartCommand called");
// server communication code.
return Service.START_STICKY;
}
}
Manifest xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex.errorhandle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.ex.errorhandle.MyApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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.ex.errorhandle.ServiceClass" />
</application>
</manifest>
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.
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" />
i have problem with sending Broadcast receive from one activity to other ..its not working my code is below..pls refer to it ..
sending class is:
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendBroadcast();
}
});
}
public void sendBroadcast(){
Intent broadcast = new Intent();
broadcast.setAction("com.unitedcoders.android.broadcasttest.SHOWTOAST");
sendBroadcast(broadcast);
}
}
and reciving class is :
public class ToastDisplay extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("asdasd","sdasdasd");
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "%%%%%%%%%%%%received", Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(SendBroadcast.BROADCAST_ACTION);
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
}
Manifest file is ::::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SendBroadcast"
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=".ToastDisplay">
<intent-filter>
<action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"> </action>
</intent-filter>
</activity>
</application>
</manifest>
you have not registered your Receiver in Manifest :registered as
<receiver android:name="receiver">
<intent-filter>
<action
android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/>
</intent-filter>
</receiver>
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 ...
}