make your android app boot at device start-up

If you want your app to start-up automatically when Android boots up you need to the following

#1: add the following to your android manifest file


<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

view raw

gistfile1.xml

hosted with ❤ by GitHub

This will register for a boot complete receiver event and ask for its permission.

#2 Add a the following java class


public class BootUpReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

 

Converting UNIX timestamp to seconds, minutes, hours, days, weeks, month, years, decades ago

If you have UNIX time-stamps and you want convert it to show how may seconds, minutes, hours, weeks, months, years or decades ago it is from now you may use the following snippet of code. It accepts UNIX time-stamp in long format, In case you are getting UNIX time-stamps in String format you have to convert it to long. It will return a string with either seconds, minutes , hours , days, weeks, months, years , or decades ago. The code is written in java for an android app but can be ported to any language. Here is the code which is also available on github.


public static String caluculateTimeAgo(long timeStamp) {
long timeDiffernce;
long unixTime = System.currentTimeMillis() / 1000L; //get current time in seconds.
int j;
String[] periods = {"s", "m", "h", "d", "w", "m", "y", "d"};
// you may choose to write full time intervals like seconds, minutes, days and so on
double[] lengths = {60, 60, 24, 7, 4.35, 12, 10};
timeDiffernce = unixTime – timeStamp;
String tense = "ago";
for (j = 0; timeDiffernce >= lengths[j] && j < lengths.length – 1; j++) {
timeDiffernce /= lengths[j];
}
return timeDiffernce + periods[j] + " " + tense;
}

 

 

 

How to play ringtone/alarm/notification sound in Android

Playing the default notification sound of the device is the most effective way to notify a user. This can be established by getting the Uri of the audio file from RingtoneManager. To play default alarm tone , ringtone or notification sound from an android app, the following code snippet is useful.

Play Notification Tone

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Play Alarm Tone

To play an Alarm-tone change TYPE_NOTIFICATION to TYPE_ALARM.

try {
 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
 Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
 r.play();
 } catch (Exception e) {
 e.printStackTrace();
 }

Set & Play a Tone from Raw resources folder

To set a file from the raw folder of your app just change the uri to get the desired file from raw resources. If you have included a tone called sownd.mp3 in your raw resources folder, all you have to do is change

 Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

to

  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");

The code should look like this now

try {
  Uri path = Uri.parse("android.resource://"+getPackageName()+"/raw/sound.mp3");
  // The line below will set it as a default ring tone replace
  // RingtoneManager.TYPE_RINGTONE with RingtoneManager.TYPE_NOTIFICATION
  // to set it as a notification tone
  RingtoneManager.setActualDefaultRingtoneUri(
                    getApplicationContext(), RingtoneManager.TYPE_RINGTONE,
                    path);
  Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), path); 
  r.play();
 } 
catch (Exception e) {
 e.printStackTrace(); 
}
 

Soft-Uart Tx only using software delays only

I was working on a code for a module on my GSM gateway today , for which I had given 1 pin of micro-controller to use as software Tx. Software UARTs usually uses timers to make them robust, but I had already used them all. So I decided to write a code using software delays.

The UART logic is inverted , so to send logic 1 you have to send low signal  and vice versa . Here is my code , hope it might help someone else.

/*
* soft-uart Tx only without any timmer uses software delays
* the baud rate depends on the delay in us , here I am using
* 4800 with a 1 start bit, 8 databits and 1 stop bit
* if you wanna change the baud rate calculate it by 1/baud and
* modify the _delay_us();
*
* Created: 11/21/2012 1:52:37 PM
* Author: AbuUmar
*/
#include <avr/io.h>
#include <util/delay.h>
#define portlow PORTC&=~0x01
#define porthigh PORTC|=0x01
void putchar_soft(char data_soft)
{
 char bit_count=10; // 1+8+1SB
 data_soft=~data_soft;
 char secc=1;char0:
 if (secc=1)
 portlow;
 else
 porthigh;
 _delay_us(208);
 //_delay_us(208);
 for ( char i = 0; i < 8; i++ ) {
 if(data_soft & 1)
 portlow;
 else
 porthigh;
 data_soft=data_soft>>1;
 _delay_us(208);
 }
 porthigh;
 _delay_us(208);
 _delay_us(208);
 return;
}
int main(void)
{
 DDRC|=0b00000001;
 porthigh;
char inte=0;
while(1)
 {
 // example use, initializing a var to 0 and sending the data
 // with 1 sec delays
 putchar_soft(inte) ;
 _delay_ms(250);
 _delay_ms(250);
 _delay_ms(250);
 _delay_ms(250);
 inte++;
 }
}
 

iOS Libraries and tools for developers [iPhone iPod iPad]

2D Game Engines

3D Game Engines

Analytics

 

 

 

 

 

 

 

… 

 

Android Libraries and tools collection

A collection of free and opensource libraries and tools for developers that can prove to be very helpful.

 

yet another seven segment display code generator/calculator

Yesterday I made a post regarding a seven segment display code generator, One of our reader Zafer shared his work with us through the comments section. Its a nice and useful utility. Its in Turkish language but its easy to use. Download it here  from Dropbox.

Hi Muhammad
I follow your site with interest.
I’ve prepared the following program. hopefully be useful.
Zafer
http://yadi.sk/d/0PqFkSf2P9gJr

 

 

How to filter logcat in Android Studio?

In logcat of Android Studio there is usually too much output, sometimes a developer would want to filter out these results so that he only gets logs from the application he is debugging. This can be achieved by the following method.
On the left side (right next to the tabs) is an icon with green arrows – it can be toggled on/off to display only logcat from the process selected in the list 🙂

aJJJa

UPDATE:
as of android studio ver 0.4.5 u will get messages from the app that is running only by default. Log cat has a new option (ON by default) which creates an application filter automatically such that only the launched application’s output is shown
 

 

GSM Modem/Module not responding to AT commands after firmware Upgrade??

I get a lot of queries about this issue, so I thought I should write a small post about it. For example the latest one was

Hello Muhammad,
I recently update SIM900 to firmware 1137B12SIM900M64_ST.cla using “Simcom – sim900 Customer flash loader V1.01″ at the baud rate 1498000. Everything went perfectly like but after I restart the module I get “þIIIIþþþ” response from SIM900 at 115200 baud rate, I could not get any AT commands working. Kindly do help me.

Some modems support AutoBaud by default. Auto Baud feature allows the modem to be used with any baud rate, and for this to be used the Modem waits for a input string “ATr” it uses this string to detect the baud rate being used. As the manual describes the modem when in Auto Baud will be looking for this string after it is powered and this should be in CAPS otherwise modem wont be able to detect the correct baud rate. Sim900 has the auto baud by default. Some modems don not support Auto Baud so for these modems after the firmware has been updated, you should use the default baud rate, 9600 and 115200 are the most common.

if you want to fix or change the baud rate you should use the AT+IPR command. For example if you need to fix it to 4800.

 AT+IPR=4800r
 

Keeping track of time in embedded applications: millis();

Its very handy to keep track of time in embedded programs. In this post I will implement a function called millis() which can be used to track time.  Arduino users will be familiar with this one. I would be doing it for AVR MCUs you can easily port it for others. this function returns the number of milliseconds since the MCU began running the current program. This number will overflow (go back to zero), after approximately 50 days.
It uses a hardware timer , in this post i will use timer0 . The first step is to initialize timer0 and interupts. lets start.

void timer0(){
  // To set clock:
  // 1MHZ is 1,000,000 ticks per second
  // 1000 milli in 1 second
  // xMHZ = 1000millis
  // so MHZ/millis gives # HZ per millis
  // (HZ/millis)/prescaler= Top counter number

  // EG:for 8MHZ clock
  // 8000000/1000
  // 8000.0000000000
  // 8000/256
  // 31.2500000000 TOP counter

  //set CTC (clear timer on compare match mode)
  TCCR0A = (1< <WGM01);
  //sets prescaler clkIO/256  ***THIS MIGHT CAUSE ISSUES SETS FOR ALL CLOCKS**!!!!
  TCCR0B = (1<<CS02);
  //sets interrupt enable for OCF0A (TIMER_COMPA_vect)
  TIMSK0 = (1<<OCIE0A);
  //sets TOP counter match A at 31
  OCR0A = 31;
}


volatile uint32_t millis()
{
 uint32_t mill;
 uint8_t oldSREG = SREG;
 // remember last value of interrupts
 // disable interrupts while we read timer0_millis or we might get an
 // inconsistent value (e.g. in the middle of a write to timer0_millis)
 cli();
 mill = millis_count;
 SREG = oldSREG; // rewrite reg value to turn back on interrupts
 return mill;
}

In the code shown above we have initialized timer/counter 0 to make an interrupt after every millisecond. Next we have to update our millisecond count.

//interrupt declaration
ISR(TIMER0_COMPA_vect)
{
  ++millis_count;
  //OCR0A = 10; //sets upper breakpoint A
}

That’s it. Lets see how to use it! First we copy the current value in milis() to a variable.

uint32_t starttime=millis();

and later we compare the new values with the start value. Here’s an example of a 25 second.

if(millis()-starttime > 25000)
{
  // some code here
}

Note that the parameter for millis() is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints.

There are a number of ways you can use this. Hope this post will help you