设计一个APP应用程序程序,要求实现以下功能: (1)主活动加载一个布局文件,该布局文件上有一个文本编辑款,三个按钮。第一个按钮显示“活动间跳转”,第二个按钮显示“启动服务”,第三个按钮显示“发送广播” (2)点击“活动间跳转”按钮后,跳转到另一个活动,并显示主活动上文本编辑框中的内容。 (3)点击“启动服务”按钮后,启动一个服务,在该服务中弹出一个服务已启动的提示框。 (4)定义一个广播接收器。点击“发送广播”按钮,发送一条广播,广播接收器接收发送的广播,并利用Log类输出调试信息。给出详细的代码
时间: 2023-11-26 20:03:50 浏览: 80
以下是一个简单的实现上述需求的APP应用程序代码:
1. 主活动(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 buttonJump, buttonStartService, buttonSendBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
buttonJump = findViewById(R.id.buttonJump);
buttonStartService = findViewById(R.id.buttonStartService);
buttonSendBroadcast = findViewById(R.id.buttonSendBroadcast);
buttonJump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("text", editText.getText().toString());
startActivity(intent);
}
});
buttonStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
buttonSendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
}
```
2. 第二个活动(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.textView);
Intent intent = getIntent();
String text = intent.getStringExtra("text");
textView.setText(text);
}
}
```
3. 服务(MyService.java):
```java
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 int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
```
4. 广播接收器(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, "Received broadcast: " + intent.getAction());
}
}
```
5. 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>
```
在布局文件(activity_main.xml)中,我们需要添加一个文本编辑框和三个按钮,代码如下:
```xml
<?xml version="1.0" encoding="utf-8"?>
<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/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type some text here" />
<Button
android:id="@+id/buttonJump"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity Jump" />
<Button
android:id="@+id/buttonStartService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service" />
<Button
android:id="@+id/buttonSendBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Broadcast" />
</LinearLayout>
```
这样就完成了一个简单的APP应用程序,可以实现在主活动中输入文本,点击按钮实现活动间跳转、启动服务和发送广播等功能。
阅读全文