Skip to content

Instantly share code, notes, and snippets.

@ckurtm
Created November 2, 2015 06:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ckurtm/ea4ba85c1dfc7be4a43b to your computer and use it in GitHub Desktop.
Save ckurtm/ea4ba85c1dfc7be4a43b to your computer and use it in GitHub Desktop.
Example on how to use adb to start an Activity,BroadcastReceiver or Service from adb and include intent extras too. for Activity: adb shell am start -n "com.peirr.test/com.peirr.test.MyActivity" --es name "John" --ei age 30 for BroadcastReceiver adb shell am broadcast -n "com.peirr.test/com.peirr.test.MyReceiver" --es name "John" --ei age 30 for…
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.peirr.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />
<activity android:name=".MyActivity"
android:exported="true"
/>
</application>
</manifest>
package com.peirr.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView lname = (TextView) findViewById(R.id.name);
TextView lage = (TextView) findViewById(R.id.age);
Intent intent = getIntent();
if (intent != null) {
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age",0);
lname.setText(name);
lage.setText(String.valueOf(age));
}
}
}
package com.peirr.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {}
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age",0);
Toast.makeText(context,"MyReceiver Received: [name:"+name+"] [age:"+age+"]",Toast.LENGTH_SHORT).show();
}
}
}
package com.peirr.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age",0);
Toast.makeText(this, "MyService Received: [name:" + name + "] [age:" + age + "]", Toast.LENGTH_SHORT).show();
}
return super.onStartCommand(intent, flags, startId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment