Skip to content

Instantly share code, notes, and snippets.

@donnfelker donnfelker/AndroidManifest.xml Secret
Created Feb 26, 2016

Embed
What would you like to do?
Background Processing with an IntentService (Caster.IO)
<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.caster.androidfundamentals"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MyIntentService" />
</application>
</manifest>
package io.caster.androidfundamentals;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++) {
Intent intent = new Intent(this, MyIntentService.class);
intent.putExtra("some_value", i);
startService(intent);
}
Log.d("INTENT_SERVICE_EX", "Done firing off service intents");
}
}
package io.caster.androidfundamentals;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
*/
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
/// Do work here
try {
if(intent.getExtras().containsKey("some_value")) {
int someValue = intent.getIntExtra("some_value", 0);
Log.d("INTENT_SERVICE_EX", String.valueOf(someValue));
}
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.