设计一个APP应用程序程序,要求实现以下功能: (1)主活动加载一个布局文件,该布局文件上有一个文本编辑款,三个按钮。第一个按钮显示“活动间跳转”,第二个按钮显示“启动服务”,第三个按钮显示“发送广播” (2)点击“活动间跳转”按钮后,跳转到另一个活动,并显示主活动上文本编辑框中的内容。 (3)点击“启动服务”按钮后,启动一个服务,在该服务中弹出一个服务已启动的提示框。 (4)定义一个广播接收器。点击“发送广播”按钮,发送一条广播,广播接收器接收发送的广播,并利用Log类输出调试信息。
时间: 2023-11-25 07:04:15 浏览: 36
以下是一个实现上述功能的示例代码:
MainActivity.java:
```java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button jumpButton, startServiceButton, sendBroadcastButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
jumpButton = findViewById(R.id.jump_button);
startServiceButton = findViewById(R.id.start_service_button);
sendBroadcastButton = findViewById(R.id.send_broadcast_button);
jumpButton.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);
}
});
startServiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
sendBroadcastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
}
```
activity_main.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here" />
<Button
android:id="@+id/jump_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity Jump" />
<Button
android:id="@+id/start_service_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service" />
<Button
android:id="@+id/send_broadcast_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast" />
</LinearLayout>
```
SecondActivity.java:
```java
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
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);
Intent intent = getIntent();
String text = intent.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"
android:gravity="center"
android:textSize="24sp" />
```
MyService.java:
```java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
}
```
MyBroadcastReceiver.java:
```java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Broadcast received");
}
}
```
AndroidManifest.xml:
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
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"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcast.MY_BROADCAST" />
</intent-filter>
</receiver>
</application>
</manifest>
```
在代码中,我们实现了以下功能:
1. 在主活动中显示一个文本编辑框和三个按钮。
2. 点击“活动间跳转”按钮后,跳转到另一个活动,并显示主活动上文本编辑框中的内容。
3. 点击“启动服务”按钮后,启动一个服务,在该服务中弹出一个服务已启动的提示框。
4. 定义一个广播接收器。点击“发送广播”按钮,发送一条广播,广播接收器接收发送的广播,并利用Log类输出调试信息。
要注意的是,我们在 AndroidManifest.xml 文件中注册了 MyBroadcastReceiver 接收器,并定义了一个名为 "com.example.broadcast.MY_BROADCAST" 的自定义广播。在发送广播时,我们需要使用相同的 action 名称。