I have to create two android apps.
App1 - takes an input message (i.e. “Hello World”) from the user and App2 prints out the message to the Console viewable via ADB Logcat. The message from App1 should be sent to App2 via Intents. App2 should be a Service
I am not sure whether to use Service
or IntentService
for App2. If I create a service for App2. Would I be able to use it by using Implicit Intent like this:
Intent serviceIntent = new Intent("com.example.vinitanilgaikwad.app2");
bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);
Could you please advise me how should I proceed?
My App1 has following source code classes.
App1: DisplayMessageActivity
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);
Intent serviceIntent = new Intent("com.example.vinitanilgaikwad.app2");
bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);
startService(serviceIntent);
}
Messenger myService = null;
boolean isBound;
private ServiceConnection myConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
myService = new Messenger(service);
isBound = true;
}
public void onServiceDisconnected(ComponentName className) {
myService = null;
isBound = false;
}
};
@Override
public void onDestroy() {
super.onDestroy();
unbindService(myConnection);
}
public void sendMessage(View view) {
// if (!isBound) return;
Message msg = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("MyString", "Vinit");
msg.setData(bundle);
try {
myService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
App2 has the service implementation.
App2: MessengerService
package com.example.vinitanilgaikwad.app2;
public class MessengerService extends Service {
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.i("dddsds","fsdfdsfsfs");
Bundle data = msg.getData();
String dataString = data.getString("MyString");
Toast.makeText(getApplicationContext(),
dataString, Toast.LENGTH_SHORT).show();
Log.d("Me123",dataString);
}
}
final Messenger myMessenger = new Messenger(new IncomingHandler());
@Override
public IBinder onBind(Intent intent) {
return myMessenger.getBinder();
}
}
App2: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vinitanilgaikwad.app2">
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MessengerService"
>
<intent-filter>
<action android:name="com.example.vinitanilgaikwad.app2"></action>
</intent-filter>
</service>
</application>
</manifest>
Still App1 is not able to connect to Service in App2. Adb logcat does not print the message.
Can someone help? I am new to Android development.