Create service by native implementation

356 views
Skip to first unread message

Godwin

unread,
Apr 27, 2014, 6:23:05 AM4/27/14
to codenameone...@googlegroups.com
Has anybody been able to create a Service from his CN1 application by native implementation? I have been able to do this with Native Android but I have not succeeded in adding it to my CN1 application.

If you have, please assist me with the steps you took.

Shai Almog

unread,
Apr 27, 2014, 2:45:02 PM4/27/14
to codenameone...@googlegroups.com
Create service class. Place it in the hierarchy under native/android/your/package/name.
Add your xml fraction related to the service under the build argument android.xapplication.

Godwin

unread,
Apr 28, 2014, 12:50:30 PM4/28/14
to codenameone...@googlegroups.com
This is what I did:

1. I right clicked on native/android and created a package (com.mycom.myapp) but it created under src folder. I copied the folder to native/android.
2. I right clicked on the package folder structure and created a java class for the Service. I added it to the package I created above, it still did not appear under native/android/myPackage but under src/myPackage.
3. I copied the file to the directory under native/android.
4. I made the class to extend Service then wrote the code below in it :

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Vibrator;
import com.mtechcomm.myFiends.MyFriendsManager;

public class RMService extends Service {


   
@Override
   
public IBinder onBind(Intent intent) {
       
// TODO Auto-generated method stub
       
return null;
   
}

   
@Override
   
public void onDestroy() {
       
// TODO Auto-generated method stub

       
super.onDestroy();
       
   
}

   
@Override
   
public void onCreate() {
       
// TODO Auto-generated method stub
       
super.onCreate();
   
}

   
@Override
   
public int onStartCommand(Intent intent, int flags, int startId) {
        android
.content.Context ctx = (android.content.Context) MyFriendsManager.getContext();
       
Vibrator vibrator = (Vibrator) ctx.getSystemService(android.content.Context.VIBRATOR_SERVICE);
        vibrator
.vibrate(2000);

        com
.mtechcomm.myFiends.MyFriendsManager manager = new com.mtechcomm.myFiends.MyFriendsManager();
        manager
.displayNotification();

       
return START_STICKY;
   
}

}

Then in my native Implementation class, I wrote:

public void beginService() {
       
final Activity activity = com.codename1.impl.android.AndroidNativeUtil.getActivity();
        com
.codename1.impl.android.CodenameOneActivity ctx = (com.codename1.impl.android.CodenameOneActivity) MyFriendsManager.getContext();
       
       
Calendar calendar = Calendar.getInstance();
        calendar
.set(Calendar.HOUR_OF_DAY, 4);
        calendar
.set(Calendar.MINUTE, 00);
        calendar
.set(Calendar.SECOND, 0);
        calendar
.set(Calendar.AM_PM, Calendar.PM);

        android
.content.Intent intent = new android.content.Intent(ctx, MyFriendsManager.class);
        android
.app.PendingIntent pintent = android.app.PendingIntent.getService(ctx, 0, intent, 0);
        android
.app.AlarmManager alarm = (android.app.AlarmManager) ctx.getSystemService(android.content.Context.ALARM_SERVICE);
        alarm
.set(android.app.AlarmManager.RTC, calendar.getTimeInMillis(), pintent);
       
   
}

On start of My app, I called the beginService().

This does not work as I do not receive any notification at the time I set it. Please what am I doing wrong?

NB: The displayNotification Method is :
 public void displayNotification() {
       
Display.getInstance().notifyStatusBar("My Application", "Today's Events", "You have birthdays today", true, true, null);
   
}

Chen Fishbein

unread,
Apr 28, 2014, 4:45:34 PM4/28/14
to codenameone...@googlegroups.com
if what you want is to schedule some task on the AlarmManager, you should create a BroadcastReceiver and make sure to add it's entries to the manifest.
also once this task is triggered you are out of the app context you need to communicate back through intents or through SharedPreferences

Godwin

unread,
Apr 29, 2014, 6:50:04 AM4/29/14
to codenameone...@googlegroups.com

When I create a Broadcast Receiver class, in which directory do I put it? Like the picture above, Do I place the class under native/android/packageName?

Chen Fishbein

unread,
Apr 29, 2014, 7:16:14 AM4/29/14
to codenameone...@googlegroups.com
Yes, all classes that uses android api must be under the native/android/... dirs

Godwin

unread,
May 1, 2014, 12:05:15 PM5/1/14
to codenameone...@googlegroups.com
I have not been able to get this to work still.
These are my problems:

1. I have a native interface already and I am using it for Writing to the phone's Calendar.
2. Do I need to create another native interface that extends Service(in the case of Android)? what am I going to put inside NativeImpl.java class if I create another native interface?
3. I am using an alarm manager and pendingintent, where do I put the method that calls the alarm manager to start?

Please help out.

Shai Almog

unread,
May 1, 2014, 2:10:14 PM5/1/14
to codenameone...@googlegroups.com
2. No. Just define a service class in the native directory.
3. In the android.xapplication section, just paste your xml snippet there.

Chen Fishbein

unread,
May 1, 2014, 3:03:50 PM5/1/14
to codenameone...@googlegroups.com
You need a BroadcastReceiver not a service.

Try this in your native impl:

    private android.app.AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    public void startAlarm() {
        Activity context = AndroidNativeUtil.getActivity();
        
        alarmMgr = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MyReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        alarmMgr.setRepeating(android.app.AlarmManager.RTC, System.currentTimeMillis() + 2000, 1000*10, alarmIntent);
    }

Add your Receiver:

public class MyReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        System.out.println("cn1 got it!!!!!");
    }
}

Then add it to your manifest:

android.xapplication = <receiver android:name="MyReceiver"></receiver>


Godwin

unread,
May 2, 2014, 7:10:58 AM5/2/14
to codenameone...@googlegroups.com

Thanks so much for the help rendered, they are very useful.
I have created the files as suggested and these are my classes and methods :

public void onReceive(Context context, Intent intent) {

        com
.codename1.impl.android.CodenameOneActivity ctx = (com.codename1.impl.android.CodenameOneActivity) MyFriendsManager.getContext();

        android
.os.Vibrator vibrator = (android.os.Vibrator) ctx.getSystemService(android.content.Context.VIBRATOR_SERVICE);

        vibrator
.vibrate(2000);

            com
.mtechcomm.myFiends.MyFriendsManager manager = new com.mtechcomm.myFiends.MyFriendsManager();
            manager
.displayNotification();
   
}

However, the device neither vibrates nor displays the notification in the Application.java class.

Godwin

unread,
May 2, 2014, 11:01:58 AM5/2/14
to codenameone...@googlegroups.com
This is an extract from my log cat :

AlarmManager(  498): Native set alarm :Alarm{4288d2b8 type 1 com.mtechcomm.myFiends}
V/AlarmManager(  498): Native set alarm :Alarm{4288d2b8 type 1 com.mtechcomm.myFiends}
I/AlarmManager(  498): reset poweroff alarm none
V/ActivityManager(  498): Broadcast: Intent { flg=0x14 cmp=com.mtechcomm.myFiends/userclasses.MyReceiver (has extras) } ordered=true userid=0 callerApp=null

Is it any useful information?

Shai Almog

unread,
May 2, 2014, 1:14:51 PM5/2/14
to codenameone...@googlegroups.com
Did you add the vibrate permission in the build arguments.

Godwin

unread,
May 2, 2014, 4:47:24 PM5/2/14
to codenameone...@googlegroups.com
Yes I added :
<uses-permission android:name="android.permission.VIBRATE" />
to the android permissions.

The onReceive() never gets called(It does not print "System.out.println("*************  Just got into the OnReceive Method  ******************")");

Shai Almog

unread,
May 3, 2014, 3:46:57 AM5/3/14
to codenameone...@googlegroups.com
Maybe Chen knows.

Chen Fishbein

unread,
May 4, 2014, 3:50:38 AM5/4/14
to codenameone...@googlegroups.com
Can you share the code that schedule the Alarm?

Godwin

unread,
May 4, 2014, 5:15:09 AM5/4/14
to codenameone...@googlegroups.com
This schedules the Alarm:

public String startAlarm() {
       
Activity context = com.codename1.impl.android.AndroidNativeUtil.getActivity();


        alarmMgr
= (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
       
Intent intent = new Intent(context, MyReceiver.class);

        alarmIntent
= android.app.PendingIntent.getBroadcast(context, 0, intent, 0);

       
Calendar calendar = Calendar.getInstance();
        calendar
.set(Calendar.HOUR_OF_DAY, 10);
        calendar
.set(Calendar.MINUTE, 50);
        calendar
.set(Calendar.SECOND, 00);
        alarmMgr
.setRepeating(android.app.AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 10, alarmIntent);
       
//alarmMgr.setRepeating(android.app.AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60 * 10, alarmIntent);
       
return "started";
   
}

and this one is the receiver :

public class MyReceiver extends BroadcastReceiver {


   
@Override

   
public void onReceive(Context context, Intent intent) {

       
System.out.println("*************  Just got into the OnReceive Method  ******************");
       
//com.codename1.impl.android.CodenameOneActivity ctx = (com.codename1.impl.android.CodenameOneActivity) MyFriendsManager.getContext();
        android
.content.Context ctx = (android.content.Context) MyFriendsManager.getContext();

        android
.os.Vibrator vibrator = (android.os.Vibrator) ctx.getSystemService(android.content.Context.VIBRATOR_SERVICE);
        vibrator
.vibrate(2000);

       
System.out.println("*************  Just got here now  ******************");

            com
.mtechcomm.myFiends.MyFriendsManager manager = new com.mtechcomm.myFiends.MyFriendsManager();
            manager
.displayNotification();
   
}
}

Godwin

unread,
May 5, 2014, 5:47:47 PM5/5/14
to codenameone...@googlegroups.com
Is there any luck on this?

Chen Fishbein

unread,
May 6, 2014, 1:44:04 AM5/6/14
to codenameone...@googlegroups.com
Not sure why it doesn't work for you, It works just fine for me.
I'd suggest you to build with source and then open the Android project in eclipse and debug this.

Godwin

unread,
May 7, 2014, 3:49:16 AM5/7/14
to codenameone...@googlegroups.com
I have been checking my code for why this doesn't work and I would like to ask:

1. I created a native interface earlier that I used for setting Calendar events. It is inside the CalendarEventImpl.java that I added the startAlarm() method. Is this right?
2. I created a Class outside the CalendarEventImpl.java as MyReceiver.java Class that extends BroadcastReceiver. Is this right?

Please give your advice.

bo11...@163.com

unread,
May 7, 2014, 6:01:22 AM5/7/14
to codenameone...@googlegroups.com
The problem has been resolved, thank you very much

Godwin

unread,
May 7, 2014, 6:29:02 AM5/7/14
to codenameone...@googlegroups.com
@Bo Lin, were you able to make it work for you? How did you do it?

Shai Almog

unread,
May 7, 2014, 10:30:39 AM5/7/14
to codenameone...@googlegroups.com
Both seem to be fine. I suggest logging from DDMS.

 I'm assuming Bo Lin posted to the wrong thread.

Godwin

unread,
May 7, 2014, 2:00:32 PM5/7/14
to codenameone...@googlegroups.com
This is an extract from the logcat :

I/System.out( 6958): We are testing the done button
I
/System.out( 6958): All selected []
V
/AudioHardwareMSM76XXA(  126): MBADRC and ADRC Disabled
W
/ResourceType( 6958): No package identifier when getting value for resource number 0x00000000
V
/AudioHardwareMSM76XXA(  126): open driver
V
/AudioHardwareMSM76XXA(  126): get config
V
/AudioHardwareMSM76XXA(  126): set config
V
/AudioHardwareMSM76XXA(  126): buffer_size: 4800
V
/AudioHardwareMSM76XXA(  126): buffer_count: 2
V
/AudioHardwareMSM76XXA(  126): channel_count: 2
V
/AudioHardwareMSM76XXA(  126): sample_rate: 44100
V
/AudioHardwareMSM76XXA(  126): MBADRC Enabled
V
/AudioHardwareMSM76XXA(  126): MBADRC Enabled 16
I
/System.out( 6958): showKeyboard false
I
/System.out( 6958): showKeyboard false
D
/-heap   ( 6958): GC_FOR_ALLOC freed 646K, 34% free 9765K/14691K, paused 70ms
I
/Codename One( 6958): Resource not found: theme_phone.ovr
I
/Codename One( 6958): Resource not found: theme_android.ovr
I
/Codename One( 6958): Resource not found: theme_android-phone.ovr
W
/ResourceType( 6958): No package identifier when getting value for resource number 0x00000000
E
/Crashlytics( 7014): Failed to retrieve settings from https://settings.crashlytics.com/spi/v2/platforms/android/apps/com.twitter.android/settings


 Can anything be deduced from this?

Godwin

unread,
May 19, 2014, 5:53:03 AM5/19/14
to codenameone...@googlegroups.com
I have not gotten this to work yet. I have a logcat message that I would like you to check out for me.


I/System.out(16399): *************** called the Alarm ******************
D/dalvikvm(16399): GC_CONCURRENT freed 1405K, 28% free 7744K/10648K, paused 3ms+4ms, total 62ms
I/System.out(16399): *************** Started at 1400490626589
V/AlarmManager(  495): Native set alarm :Alarm{4207e780 type 0 com.mtechcomm.myFiends}
V/AlarmManager(  495): Native set alarm :Alarm{41fd3a80 type 0 com.google.android.gms}
I/AlarmManager(  495): reset poweroff alarm none
V/ActivityManager(  495): Broadcast: Intent { flg=0x14 cmp=com.mtechcomm.myFiends/userclasses.CalendarEventImpl$MyReceiver (has extras) } ordered=true userid=0 callerApp=null

Please what do I do from here?

This is my code

 public void calAlarm(){
       
Activity context = com.codename1.impl.android.AndroidNativeUtil.getActivity();
       
MyReceiver mr = new MyReceiver();
       
System.out.println("*************** called the Alarm ******************");
        mr
.startAlarm(context);

   
}
   
public class MyReceiver extends BroadcastReceiver {

       
@Override
       
public void onReceive(Context context, Intent intent) {

           
Log.d("OnStartCommand", "Service has started *******************");
           
String message = intent.getStringExtra("message");
           
System.out.println("*************  Just got into the OnReceive Method  ******************" + message);
           
           
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

            vibrator
.vibrate(2000);
           
System.out.println("*************  Just got here now  ******************");
            com
.mtechcomm.myFiends.MyFriendsManager manager = new com.mtechcomm.myFiends.MyFriendsManager();
            manager
.displayNotification();
       
}


       
public void startAlarm(Context context) {            
            alarmMgr
= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

           
Intent intent = new Intent(context, MyReceiver.class);

            alarmIntent
= PendingIntent.getBroadcast(context, 0, intent, 0);
           
Calendar calendar = Calendar.getInstance();
            calendar
.set(Calendar.HOUR_OF_DAY, 11);
            calendar
.set(Calendar.MINUTE, 55);
            calendar
.set(Calendar.SECOND, 00); //calendar.getTimeInMillis()
           
System.out.println("*************** Started at "+System.currentTimeMillis());
            alarmMgr
.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), alarmIntent);            
       
}
   
}

Chen Fishbein

unread,
May 19, 2014, 2:47:07 PM5/19/14
to codenameone...@googlegroups.com
Nothing suspicious in the Log.
Did you tried this?did it worked?

    private android.app.AlarmManager alarmMgr;
    private PendingIntent alarmIntent;

    public void startAlarm() {
        Activity context = AndroidNativeUtil.getActivity();
        
        alarmMgr = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MyReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

Godwin

unread,
May 26, 2014, 9:17:01 AM5/26/14
to codenameone...@googlegroups.com
I have been trying this code since then but not been able to figure out why the onRecieve() never gets called.From the Logcat, the Alarm is shown to be triggered but the onReceive is never called. This is an extract from the Logcat :

I/AlarmManager(  381): trigger Alarm{41c97040 type 0 com.mtechcomm.myFiends} [ty
pe
=0 when=(2014-05-26 14:06:33) repeatInt=10000 PendingIntent{418ddb60: PendingI
ntentRecord
{41add2b8 com.mtechcomm.myFiends broadcastIntent}}]

I
/AlarmManager(  381): trigger Alarm{41c97040 type 0 com.mtechcomm.myFiends} [ty
pe
=0 when=(2014-05-26 14:06:43) repeatInt=10000 PendingIntent{418ddb60: PendingI
ntentRecord
{41add2b8 com.mtechcomm.myFiends broadcastIntent}}]

I
/AlarmManager(  381): trigger Alarm{41c97040 type 0 com.mtechcomm.myFiends} [ty
pe
=0 when=(2014-05-26 14:06:53) repeatInt=10000 PendingIntent{418ddb60: PendingI
ntentRecord
{41add2b8 com.mtechcomm.myFiends broadcastIntent}}]

I
/AlarmManager(  381): trigger Alarm{41c97040 type 0 com.mtechcomm.myFiends} [ty
pe
=0 when=(2014-05-26 14:07:03) repeatInt=10000 PendingIntent{418ddb60: PendingI
ntentRecord
{41add2b8 com.mtechcomm.myFiends broadcastIntent}}]

Any Ideas?

Godwin

unread,
May 27, 2014, 11:17:11 AM5/27/14
to codenameone...@googlegroups.com
It looks like I finally made a headway here. After registering the Receiver programmatically, the onReceive() method got called. I learnt that I should also Unregister the receiver after registering it, so at which point do I unregister it since the Alarm repeats?
Reply all
Reply to author
Forward
0 new messages