(1)主活动加载一个布局文件,该布局文件上有一个文本编辑款,三个按钮。第一个按钮显示“活动间跳转”,第二个按钮显示“启动服务”,第三个按钮显示“发送广播” (2)点击“活动间跳转”按钮后,跳转到另一个活动,并显示主活动上文本编辑框中的内容。 (3)点击“启动服务”按钮后,启动一个服务,在该服务中弹出一个服务已启动的提示框。 (4)定义一个广播接收器。点击“发送广播”按钮,发送一条广播,广播接收器接收发送的广播,并利用Log类输出调试信息。
时间: 2023-11-25 07:04:15 浏览: 121
以下是实现上述功能的代码:
主活动(MainActivity.java):
```java
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
Button btnJump = findViewById(R.id.btn_jump);
btnJump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("text", text);
startActivity(intent);
}
});
Button btnStartService = findViewById(R.id.btn_start_service);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
Button btnSendBroadcast = findViewById(R.id.btn_send_broadcast);
btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
}
```
布局文件(activity_main.xml):
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_jump"
android:text="活动间跳转"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_start_service"
android:text="启动服务"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_send_broadcast"
android:text="发送广播"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
第二个活动(SecondActivity.java):
```java
public class SecondActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = findViewById(R.id.text_view);
String text = getIntent().getStringExtra("text");
textView.setText(text);
}
}
```
布局文件(activity_second.xml):
```xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
服务(MyService.java):
```java
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服务已启动", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
```
广播接收器(MyBroadcastReceiver.java):
```java
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "接收到广播:" + intent.getAction());
}
}
```
在 AndroidManifest.xml 文件中注册服务和广播接收器:
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity android:name=".SecondActivity" />
<service android:name=".MyService" />
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcast.MY_BROADCAST" />
</intent-filter>
</receiver>
</application>
</manifest>
```
阅读全文