zoukankan      html  css  js  c++  java
  • Android实例-调用系统APP(XE10+小米2)

    相关资料:

    1.群号383675978 

    2.http://blog.sina.com.cn/s/blog_44fa172f0101rmjt.html

    3.PS:ListView1.ItemAppearanceObjects.ItemObjects.Text.Font.Size:=20; 设置字体大小,属性面板也可以设置。代码设置时需要看一下手机支持的大小是多少。

    实例源码:

      1 unit Unit1;
      2 
      3 interface
      4 
      5 uses
      6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
      7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
      8   FMX.Controls.Presentation, FMX.Edit,
      9   Androidapi.JNI.JavaTypes,//JString使用
     10   Androidapi.JNI.GraphicsContentViewText,//JIntent使用
     11   FMX.Surfaces,//TBitmapSurface使用
     12   Androidapi.Helpers,//SharedActivity使用
     13   System.IOUtils,//TPath使用
     14   Androidapi.JNIBridge,//ILocalObject使用
     15   FMX.Helpers.Android,//JBitmapToSurface使用
     16   System.Generics.Collections,//TList使用
     17   FMX.ListView.Types, FMX.ListView.Appearances,
     18   FMX.ListView.Adapters.Base, FMX.ListView, FMX.StdCtrls, FMX.ScrollBox,
     19   FMX.Memo;
     20 
     21 type
     22   TForm1 = class(TForm)
     23     Edit1: TEdit;
     24     ListView1: TListView;
     25     Button1: TButton;
     26     Memo1: TMemo;
     27     procedure Edit1Change(Sender: TObject);
     28     procedure Edit1Typing(Sender: TObject);
     29     procedure Button1Click(Sender: TObject);
     30     procedure FormCreate(Sender: TObject);
     31     procedure ListView1ItemClick(const Sender: TObject;
     32       const AItem: TListViewItem);
     33   private
     34     { Private declarations }
     35     MainList : TList<JActivityInfo>;
     36     dictAppIcons : TDictionary<Integer, TBitmap>;
     37     //过虑方法
     38     procedure FilterListView(AListView: TListView; AFilterName: string);
     39     //打开APP方法
     40     procedure OpenApp(PackageName, AppName : JString);
     41     //获取安装的APP
     42     function GetActivityAppList: JList;
     43     function GetOrSetCashAppIcon(appInfo: JApplicationInfo): TBitmap;
     44     procedure LoadActivityInfoList(var List: TList<JActivityInfo>);
     45     procedure LoadDictonaryAppIcons(index: Integer; appInfo: JApplicationInfo;
     46       var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
     47     procedure LoadListView(listView: TListView; AppList: TList<JActivityInfo>;
     48       dictonaryAppIcons: TDictionary<Integer, TBitmap>);
     49     procedure LoadListViewBitmap(listView: TListView; AppList: TList<JActivityInfo>;
     50       var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
     51   public
     52     { Public declarations }
     53   end;
     54 
     55 const
     56     DEFAUT_INDEX: Integer = -1;
     57 
     58 var
     59   Form1: TForm1;
     60 
     61 implementation
     62 
     63 {$R *.fmx}
     64 {$R *.NmXhdpiPh.fmx ANDROID}
     65 
     66 { TForm1 }
     67 //调用打开APP
     68 procedure TForm1.Button1Click(Sender: TObject);
     69 begin
     70   OpenApp(StringToJString('com.androidillusion.videocamillusionpro'),
     71     StringToJString('com.androidillusion.videocamillusionpro.VideoillusionActivity'));
     72 end;
     73 
     74 //改变事件
     75 procedure TForm1.Edit1Change(Sender: TObject);
     76 begin
     77   if Edit1.Text = '' then
     78    FilterListView(Self.ListView1, Edit1.Text.Trim);
     79 end;
     80 
     81 //输入事件
     82 procedure TForm1.Edit1Typing(Sender: TObject);
     83 begin
     84   FilterListView(Self.ListView1, Edit1.Text.Trim);
     85 end;
     86 
     87 //过虑方法
     88 procedure TForm1.FilterListView(AListView: TListView; AFilterName: string);
     89 var
     90   i: integer;
     91   item: TListViewItem;
     92   lower: string;
     93 begin
     94   if not Assigned(AListView) then
     95     Exit;
     96   lower := AFilterName.ToLower.Trim;
     97   if lower.IsEmpty then
     98   begin
     99     if Assigned(AListView.Items.Filter) then
    100       AListView.Items.Filter := nil;
    101   end
    102   else
    103   begin
    104     AListView.ItemIndex := DEFAUT_INDEX;
    105     AListView.Items.Filter :=
    106     function(sFilter: string): Boolean
    107     begin
    108       Result := (lower.IsEmpty) or sFilter.ToLower.Contains(lower);
    109     end;
    110   end;
    111 end;
    112 
    113 procedure TForm1.FormCreate(Sender: TObject);
    114 begin
    115   LoadActivityInfoList(MainList);
    116   LoadListView(Self.ListView1, MainList, self.dictAppIcons);
    117   LoadListViewBitmap(Self.ListView1, MainList, self.dictAppIcons);
    118 end;
    119 
    120 //获取安装的APP
    121 function TForm1.GetActivityAppList: JList;
    122 var
    123   tempList: JList;
    124   Intent: JIntent;
    125   Manager: JPackageManager;
    126 begin
    127   Intent := TJIntent.Create;
    128   Intent.SetAction(TJIntent.JavaClass.ACTION_MAIN);
    129   Intent.AddCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
    130   Manager := SharedActivity.GetPackageManager;
    131   tempList := nil;
    132   tempList := Manager.QueryIntentActivities(Intent, 0);
    133   Result := tempList;
    134 end;
    135 
    136 function TForm1.GetOrSetCashAppIcon(appInfo: JApplicationInfo): TBitmap;
    137 var
    138   Drawable: JDrawable;
    139   Bitmap: JBitmap;
    140   itemBitmap: TBitmap;
    141   Surface: TBitmapSurface;
    142   saveDir: string;
    143   pngFileName: string;
    144   SaveParams: TBitmapCodecSaveParams;
    145 begin
    146   if not Assigned(appInfo) then
    147   begin
    148     Result := itemBitmap;
    149     Exit;
    150   end;
    151 
    152   saveDir := TPath.GetCachePath;
    153   pngFileName := saveDir + '/' + JStringToString(appInfo.packageName) + '.png';
    154   itemBitmap := TBitmap.Create;
    155   if not TDirectory.Exists(saveDir, False) then
    156     TDirectory.CreateDirectory(saveDir);
    157   if TFile.Exists(pngFileName) then
    158     itemBitmap.LoadFromFile(pngFileName)
    159   else
    160   begin
    161     Drawable := appInfo.loadIcon(SharedActivity.getPackageManager);
    162     Bitmap := TJBitmapDrawable.Wrap((Drawable as ILocalObject).GetObjectID).getBitmap;
    163     Surface := TBitmapSurface.Create;
    164     try
    165       if JBitmapToSurface(Bitmap, Surface) then
    166       begin
    167         itemBitmap.Assign(Surface);
    168         SaveParams.Quality := 100;
    169         itemBitmap.SaveToFile(pngFileName, @SaveParams);
    170       end;
    171     finally
    172       Surface.Free;
    173     end;
    174   end;
    175   Result := itemBitmap;
    176 end;
    177 
    178 procedure TForm1.ListView1ItemClick(const Sender: TObject;
    179   const AItem: TListViewItem);
    180 begin
    181   if not Assigned(MainList) then
    182     Exit;
    183   OpenApp(MainList.Items[AItem.Tag].applicationInfo.packageName,
    184     MainList.Items[AItem.Tag].name);
    185   Memo1.Lines.Add(JStringToString(MainList.Items[AItem.Tag].applicationInfo.packageName) + '/' + JStringToString(MainList.Items[AItem.Tag].name));
    186 end;
    187 
    188 procedure TForm1.LoadActivityInfoList(var List: TList<JActivityInfo>);
    189 var
    190   tempList: JList;
    191   i: Integer;
    192   ResolveInfo: JResolveInfo;
    193   Info: JActivityInfo;
    194   AppInfo: JApplicationInfo;
    195 begin
    196   if not Assigned(List) then
    197     List := TList<JActivityInfo>.Create;
    198   List.Clear;
    199   tempList := Self.GetActivityAppList;
    200   for i := 0 to tempList.size - 1 do
    201   begin
    202     ResolveInfo := TJResolveInfo.Wrap((tempList.get(i) as ILocalObject).GetObjectID);
    203     Info := TJActivityInfo.Wrap((ResolveInfo.activityInfo as ILocalObject).GetObjectID);
    204     AppInfo := TJApplicationInfo.Wrap((Info.applicationInfo as ILocalObject).GetObjectID);
    205     List.Add(Info);
    206   end;
    207 end;
    208 
    209 procedure TForm1.LoadDictonaryAppIcons(index: Integer;
    210   appInfo: JApplicationInfo;
    211   var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
    212 var
    213   itemBitmap : TBitmap;
    214 begin
    215   if not Assigned(dictonaryAppIcons) then
    216     dictonaryAppIcons := TDictionary<Integer, TBitmap>.Create;
    217   if not dictonaryAppIcons.ContainsKey(index) then
    218   begin
    219     itemBitmap := GetOrSetCashAppIcon(appInfo);
    220     dictonaryAppIcons.AddOrSetValue(index, itemBitmap);
    221   end;
    222 end;
    223 
    224 procedure TForm1.LoadListView(listView: TListView;
    225   AppList: TList<JActivityInfo>;
    226   dictonaryAppIcons: TDictionary<Integer, TBitmap>);
    227 var
    228   tempItem : TListViewItem;
    229   tempString, tempSubString, tempSubString2 : string;
    230   i : integer;
    231 begin
    232   if (not Assigned(listView)) or (not Assigned(AppList)) then
    233     Exit;
    234   listView.Items.Clear;
    235   listView.BeginUpdate;
    236   for I := 0 to AppList.Count - 1 do
    237   begin
    238     tempString := JStringToString(AppList.Items[i].applicationInfo.loadLabel(SharedActivity.getPackageManager).toString);
    239     tempItem := listView.Items.Add;
    240     tempItem.Text := tempString;
    241     tempItem.Tag := i;
    242   end;
    243   listView.EndUpdate;
    244 end;
    245 
    246 procedure TForm1.LoadListViewBitmap(listView: TListView;
    247   AppList: TList<JActivityInfo>;
    248   var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
    249 var
    250   i: integer;
    251 begin
    252   if (not Assigned(listView)) or (not Assigned(AppList)) then
    253     Exit;
    254   listView.BeginUpdate;
    255   for I := 0 to listView.ItemCount - 1 do
    256   begin
    257     listView.Items[i].BeginUpdate;
    258     LoadDictonaryAppIcons(i, AppList.Items[listView.Items[i].Tag].applicationInfo, dictonaryAppIcons);
    259     if Assigned(dictonaryAppIcons) and (dictonaryAppIcons.ContainsKey(i)) then
    260         listView.Items[i].Bitmap := dictonaryAppIcons.Items[i];
    261     listView.Items[i].EndUpdate;
    262     Application.ProcessMessages;
    263   end;
    264   listView.EndUpdate;
    265 end;
    266 
    267 //打开APP方法
    268 procedure TForm1.OpenApp(PackageName, AppName: JString);
    269 var
    270   Intent : JIntent;
    271   NativeComponent : JComponentName;
    272 begin
    273   Intent := TJIntent.Create;
    274   Intent.setAction(TJIntent.JavaClass.ACTION_MAIN);
    275   Intent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
    276   NativeComponent := TJComponentName.JavaClass.init(PackageName, AppName);
    277   Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK or TJIntent.JavaClass.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    278   Intent.setComponent(NativeComponent);
    279   SharedActivity.startActivity(Intent);
    280 end;
    281 
    282 end.
  • 相关阅读:
    MC9S12 硬件设计
    ESD
    选用与使用稳压二极管的介绍
    MOSFET 栅极电阻作用及其选型
    orcad常用库文件介绍
    开关电源和LDO的区别
    续流二极管的作用及选型
    为什么大电容滤低频小电容滤高频的问题
    Java常用API——时间类
    Idea问题:“marketplace plugins are not loaded”解决方案
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/5413613.html
Copyright © 2011-2022 走看看