设计一个APP应用程序程序,要求实现以下功能: (1)主活动加载一个布局文件,该布局文件上有一个文本编辑款,三个按钮。第一个按钮显示“活动间跳转”,第二个按钮显示“启动服务”,第三个按钮显示“发送广播” (2)点击“活动间跳转”按钮后,跳转到另一个活动,并显示主活动上文本编辑框中的内容。 (3)点击“启动服务”按钮后,启动一个服务,在该服务中弹出一个服务已启动的提示框。 (4)定义一个广播接收器。点击“发送广播”按钮,发送一条广播,广播接收器接收发送的广播,并利用Log类输出调试信息。
时间: 2023-11-25 20:04:15 浏览: 48
微信小程序demo:招聘伯乐:页面布局,上拉刷新
以下是实现上述功能的一个简单示例代码:
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 buttonJump = findViewById(R.id.button_jump);
buttonJump.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 buttonStartService = findViewById(R.id.button_start_service);
buttonStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
Toast.makeText(MainActivity.this, "服务已启动", Toast.LENGTH_SHORT).show();
}
});
Button buttonSendBroadcast = findViewById(R.id.button_send_broadcast);
buttonSendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcast.MY_BROADCAST");
sendBroadcast(intent);
Log.d("MainActivity", "发送广播");
}
});
}
}
```
activity_main.xml
```xml
<LinearLayout>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文本"/>
<Button
android:id="@+id/button_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="活动间跳转"/>
<Button
android:id="@+id/button_start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"/>
<Button
android:id="@+id/button_send_broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"/>
</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
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
MyService.java
```java
public class MyService extends Service {
private static final String TAG = "MyService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "服务已启动");
return super.onStartCommand(intent, flags, startId);
}
}
```
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, "接收到广播");
}
}
```
AndroidManifest.xml
```xml
<manifest>
<application>
<activity android:name=".MainActivity"/>
<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>
```
阅读全文