Android实例-调用系统APP(XE10+小米2)

时间:2023-03-09 19:10:21
Android实例-调用系统APP(XE10+小米2)

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; 设置字体大小,属性面板也可以设置。代码设置时需要看一下手机支持的大小是多少。

实例源码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.Edit,
Androidapi.JNI.JavaTypes,//JString使用
Androidapi.JNI.GraphicsContentViewText,//JIntent使用
FMX.Surfaces,//TBitmapSurface使用
Androidapi.Helpers,//SharedActivity使用
System.IOUtils,//TPath使用
Androidapi.JNIBridge,//ILocalObject使用
FMX.Helpers.Android,//JBitmapToSurface使用
System.Generics.Collections,//TList使用
FMX.ListView.Types, FMX.ListView.Appearances,
FMX.ListView.Adapters.Base, FMX.ListView, FMX.StdCtrls, FMX.ScrollBox,
FMX.Memo; type
TForm1 = class(TForm)
Edit1: TEdit;
ListView1: TListView;
Button1: TButton;
Memo1: TMemo;
procedure Edit1Change(Sender: TObject);
procedure Edit1Typing(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
private
{ Private declarations }
MainList : TList<JActivityInfo>;
dictAppIcons : TDictionary<Integer, TBitmap>;
//过虑方法
procedure FilterListView(AListView: TListView; AFilterName: string);
//打开APP方法
procedure OpenApp(PackageName, AppName : JString);
//获取安装的APP
function GetActivityAppList: JList;
function GetOrSetCashAppIcon(appInfo: JApplicationInfo): TBitmap;
procedure LoadActivityInfoList(var List: TList<JActivityInfo>);
procedure LoadDictonaryAppIcons(index: Integer; appInfo: JApplicationInfo;
var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
procedure LoadListView(listView: TListView; AppList: TList<JActivityInfo>;
dictonaryAppIcons: TDictionary<Integer, TBitmap>);
procedure LoadListViewBitmap(listView: TListView; AppList: TList<JActivityInfo>;
var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
public
{ Public declarations }
end; const
DEFAUT_INDEX: Integer = -; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} { TForm1 }
//调用打开APP
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenApp(StringToJString('com.androidillusion.videocamillusionpro'),
StringToJString('com.androidillusion.videocamillusionpro.VideoillusionActivity'));
end; //改变事件
procedure TForm1.Edit1Change(Sender: TObject);
begin
if Edit1.Text = '' then
FilterListView(Self.ListView1, Edit1.Text.Trim);
end; //输入事件
procedure TForm1.Edit1Typing(Sender: TObject);
begin
FilterListView(Self.ListView1, Edit1.Text.Trim);
end; //过虑方法
procedure TForm1.FilterListView(AListView: TListView; AFilterName: string);
var
i: integer;
item: TListViewItem;
lower: string;
begin
if not Assigned(AListView) then
Exit;
lower := AFilterName.ToLower.Trim;
if lower.IsEmpty then
begin
if Assigned(AListView.Items.Filter) then
AListView.Items.Filter := nil;
end
else
begin
AListView.ItemIndex := DEFAUT_INDEX;
AListView.Items.Filter :=
function(sFilter: string): Boolean
begin
Result := (lower.IsEmpty) or sFilter.ToLower.Contains(lower);
end;
end;
end; procedure TForm1.FormCreate(Sender: TObject);
begin
LoadActivityInfoList(MainList);
LoadListView(Self.ListView1, MainList, self.dictAppIcons);
LoadListViewBitmap(Self.ListView1, MainList, self.dictAppIcons);
end; //获取安装的APP
function TForm1.GetActivityAppList: JList;
var
tempList: JList;
Intent: JIntent;
Manager: JPackageManager;
begin
Intent := TJIntent.Create;
Intent.SetAction(TJIntent.JavaClass.ACTION_MAIN);
Intent.AddCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
Manager := SharedActivity.GetPackageManager;
tempList := nil;
tempList := Manager.QueryIntentActivities(Intent, );
Result := tempList;
end; function TForm1.GetOrSetCashAppIcon(appInfo: JApplicationInfo): TBitmap;
var
Drawable: JDrawable;
Bitmap: JBitmap;
itemBitmap: TBitmap;
Surface: TBitmapSurface;
saveDir: string;
pngFileName: string;
SaveParams: TBitmapCodecSaveParams;
begin
if not Assigned(appInfo) then
begin
Result := itemBitmap;
Exit;
end; saveDir := TPath.GetCachePath;
pngFileName := saveDir + '/' + JStringToString(appInfo.packageName) + '.png';
itemBitmap := TBitmap.Create;
if not TDirectory.Exists(saveDir, False) then
TDirectory.CreateDirectory(saveDir);
if TFile.Exists(pngFileName) then
itemBitmap.LoadFromFile(pngFileName)
else
begin
Drawable := appInfo.loadIcon(SharedActivity.getPackageManager);
Bitmap := TJBitmapDrawable.Wrap((Drawable as ILocalObject).GetObjectID).getBitmap;
Surface := TBitmapSurface.Create;
try
if JBitmapToSurface(Bitmap, Surface) then
begin
itemBitmap.Assign(Surface);
SaveParams.Quality := ;
itemBitmap.SaveToFile(pngFileName, @SaveParams);
end;
finally
Surface.Free;
end;
end;
Result := itemBitmap;
end; procedure TForm1.ListView1ItemClick(const Sender: TObject;
const AItem: TListViewItem);
begin
if not Assigned(MainList) then
Exit;
OpenApp(MainList.Items[AItem.Tag].applicationInfo.packageName,
MainList.Items[AItem.Tag].name);
Memo1.Lines.Add(JStringToString(MainList.Items[AItem.Tag].applicationInfo.packageName) + '/\' + JStringToString(MainList.Items[AItem.Tag].name));
end; procedure TForm1.LoadActivityInfoList(var List: TList<JActivityInfo>);
var
tempList: JList;
i: Integer;
ResolveInfo: JResolveInfo;
Info: JActivityInfo;
AppInfo: JApplicationInfo;
begin
if not Assigned(List) then
List := TList<JActivityInfo>.Create;
List.Clear;
tempList := Self.GetActivityAppList;
for i := to tempList.size - do
begin
ResolveInfo := TJResolveInfo.Wrap((tempList.get(i) as ILocalObject).GetObjectID);
Info := TJActivityInfo.Wrap((ResolveInfo.activityInfo as ILocalObject).GetObjectID);
AppInfo := TJApplicationInfo.Wrap((Info.applicationInfo as ILocalObject).GetObjectID);
List.Add(Info);
end;
end; procedure TForm1.LoadDictonaryAppIcons(index: Integer;
appInfo: JApplicationInfo;
var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
var
itemBitmap : TBitmap;
begin
if not Assigned(dictonaryAppIcons) then
dictonaryAppIcons := TDictionary<Integer, TBitmap>.Create;
if not dictonaryAppIcons.ContainsKey(index) then
begin
itemBitmap := GetOrSetCashAppIcon(appInfo);
dictonaryAppIcons.AddOrSetValue(index, itemBitmap);
end;
end; procedure TForm1.LoadListView(listView: TListView;
AppList: TList<JActivityInfo>;
dictonaryAppIcons: TDictionary<Integer, TBitmap>);
var
tempItem : TListViewItem;
tempString, tempSubString, tempSubString2 : string;
i : integer;
begin
if (not Assigned(listView)) or (not Assigned(AppList)) then
Exit;
listView.Items.Clear;
listView.BeginUpdate;
for I := to AppList.Count - do
begin
tempString := JStringToString(AppList.Items[i].applicationInfo.loadLabel(SharedActivity.getPackageManager).toString);
tempItem := listView.Items.Add;
tempItem.Text := tempString;
tempItem.Tag := i;
end;
listView.EndUpdate;
end; procedure TForm1.LoadListViewBitmap(listView: TListView;
AppList: TList<JActivityInfo>;
var dictonaryAppIcons: TDictionary<Integer, TBitmap>);
var
i: integer;
begin
if (not Assigned(listView)) or (not Assigned(AppList)) then
Exit;
listView.BeginUpdate;
for I := to listView.ItemCount - do
begin
listView.Items[i].BeginUpdate;
LoadDictonaryAppIcons(i, AppList.Items[listView.Items[i].Tag].applicationInfo, dictonaryAppIcons);
if Assigned(dictonaryAppIcons) and (dictonaryAppIcons.ContainsKey(i)) then
listView.Items[i].Bitmap := dictonaryAppIcons.Items[i];
listView.Items[i].EndUpdate;
Application.ProcessMessages;
end;
listView.EndUpdate;
end; //打开APP方法
procedure TForm1.OpenApp(PackageName, AppName: JString);
var
Intent : JIntent;
NativeComponent : JComponentName;
begin
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_MAIN);
Intent.addCategory(TJIntent.JavaClass.CATEGORY_LAUNCHER);
NativeComponent := TJComponentName.JavaClass.init(PackageName, AppName);
Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK or TJIntent.JavaClass.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Intent.setComponent(NativeComponent);
SharedActivity.startActivity(Intent);
end; end.