我需要创建两个安卓应用程序。
App1 - 由用户输入消息(例如“Hello World”),App2 将消息通过 Intents 发送到 App2 并在可通过 ADB Logcat 查看的控制台中打印出来。App2 应该是一个服务。
我不确定是否应该为 App2 使用 Service
还是 IntentService
。如果我为 App2 创建一个服务,我是否能够像这样使用隐式 Intent:
Intent serviceIntent = new Intent("com.example.vinitanilgaikwad.app2");
bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);
请问你能指导我如何继续吗?
我的应用1(App1)有以下源代码类。
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拥有服务实现。
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>
目前App1无法与App2中的Service建立连接,Adb logcat没有输出该信息。
有人能帮忙吗?我是Android开发新手。