如何在android开发使用ACTION

2025-03-10 14:31:41
推荐回答(2个)
回答1:

方法如下:

  1. 前面有用使用Intent来控制  Service

  2. 使用的参数是Service的类

  3. Service的启动/关闭还有另外一种方式来控制

  4. 通过Intent传入Action的参数

  5. 在manifest中注册一个Service并且设置一个action


            android:enabled="true"   

            android:exported="false"  

            android:name="com.services.sev.PlayService" >  

              

                  

              

          


注意这个name的字符串

可以在Service类中增加一个字段

public static final String ACTION = "com.example.codetest_1.action.startPlayService";  

这样 只需修改Intent的调用方法 就可以启动/关闭Service了

Intent intent = new Intent();  

            intent.setAction(PlayService.ACTION);  

            this.startService(intent);  

Intent intent = new Intent();  

            intent.setAction(PlayService.ACTION);  

            this.stopService(intent);  

回答2:

可以自定义一个ActionBar导航栏的。其实就相当于不用系统的ActionBar就像是自己定义的titlebar一样

你可以共享下面的代码:

1
2
3
4
5
6

String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

所以你的全部代码(图片+文本)需要变成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

private Uri imageUri;
private Intent intent;

imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + "ic_launcher");

intent = new Intent(Intent.ACTION_SEND);
//text
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of things
intent.setType("*/*");
//sending
startActivity(intent);
Uri imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));