如何使用颤动设置锁屏壁纸?

时间:2019-12-25 06:13:16

标签: api flutter wallpaper

我也遇到类似的问题,但是我正在使用Flutter开发我的应用程序。使用我的代码,我可以更改主屏幕墙纸,但不适用于设置锁定屏幕或同时设置两者。

还有另一个问题....我能够在Android api级别24和更高级别上设置主屏幕墙纸

MainActivity代码:

 package com.eabesh.wallpaper_app
 import android.os.Bundle
 import io.flutter.app.FlutterActivity
 import io.flutter.plugin.common.MethodChannel
 import java.io.IOException
 import android.app.WallpaperManager
 import android.graphics.BitmapFactory
 import android.os.Build
 import android.annotation.TargetApi
 import android.content.Context
 import io.flutter.plugins.GeneratedPluginRegistrant

   private const val CHANNEL = "com.eabesh.wallpaperapp/wallpapers"
    class MainActivity: FlutterActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)

    MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
        if (call.method == "setWallpaper") {
            val arguments = call.arguments as ArrayList<*>
            val setWallpaper = setWallpaper(arguments[0] as String, applicationContext, arguments[1] as Int)

            if (setWallpaper == 0) {
                result.success(setWallpaper)
            } else {
                result.error("UNAVAILABLE", "", null)
            }
        } else {
            result.notImplemented()
        }
    }
}
@TargetApi(24)
private fun setWallpaper(path: String, applicationContext: Context, wallpaperType: Int): Int {
    var setWallpaper=1
    val bitmap = BitmapFactory.decodeFile(path)
    val wm: WallpaperManager? = WallpaperManager.getInstance(applicationContext)
    setWallpaper = try {
        wm?.setBitmap(bitmap, null, true, wallpaperType)
        0
    } catch (e: IOException) {
        1
    }

    return setWallpaper
    }
 }

全屏代码:

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/painting.dart';
import 'package:photo_view/photo_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter/services.dart';
import 'package:image_downloader/image_downloader.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:dio/dio.dart';
import 'dart:io' as Io;




 class FullScreenImagePage extends StatefulWidget {
    final String imgPath;
    final String imgName;

    FullScreenImagePage(this.imgPath,this.imgName);

    @override
    _FullScreenImagePageState createState() => _FullScreenImagePageState();
        }

      class _FullScreenImagePageState extends State<FullScreenImagePage> {
      static const platform = const 
      MethodChannel('com.eabesh.wallpaperapp/wallpapers');
       bool permission = false;
         bool downloading = false;
         var progress = '';
         var _progresss;
          String _batteryLevel = 'Unknown battery level.';

          @override
           void initState() {
              // TODO: implement initState
                     super.initState();
                  //getPermission();
                  ImageDownloader.callback(onProgressUpdate: (String imageId, int 
                 progress) {
             setState(() {
                 _progresss = progress;
            print('>>>>>>>>>>>>>>>>>>$_progresss');
                });
             });
          }

         final LinearGradient backgroundGradient = new LinearGradient(
             colors: [new Color(0x10000000), new Color(0x30000000)],
            begin: Alignment.topLeft,
             end: Alignment.bottomRight);

           void toastFunction(context, message) {
          //Toast.show("Double Tap to Zoom",context, duration: Toast.LENGTH_SHORT, 
           gravity:  Toast.BOTTOM);
           Fluttertoast.showToast(
           msg: "$message",
             toastLength: Toast.LENGTH_SHORT,
             gravity: ToastGravity.BOTTOM,
            timeInSecForIos: 1,
            textColor: Colors.white,
             fontSize: 16.0);
           }

           IconData changeIcon() {
          if (_progresss == 100) {
             setState(() {
           return Icons.done;
             });
              } else {
                setState(() {
               return Icons.arrow_downward;
             });
             }
           return Icons.arrow_downward;
             }

         @override
          Widget build(BuildContext context) {
             double height = MediaQuery
            .of(context)
            .size
             .height;
            double width = MediaQuery
           .of(context)
            .size
              .width;
           //toastFunction(context, 'Double tap to Zoom');

return new Scaffold(
  body: new SizedBox.expand(
    child: new Container(
      decoration: new BoxDecoration(gradient: backgroundGradient),
      child: new Stack(
        children: <Widget>[
          new Align(
            alignment: Alignment.center,
            child: PhotoView(
              imageProvider: CachedNetworkImageProvider(widget.imgPath),
              loadingChild: CircularProgressIndicator(),
              //childSize: Size(width, height),
              //initialScale: PhotoViewComputedScale.covered*height,
              minScale: PhotoViewComputedScale.contained * 0.8,
              maxScale: PhotoViewComputedScale.covered * 1.8,
              heroAttributes: PhotoViewHeroAttributes(tag: widget.imgPath),
              //initialScale: height,
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: new Align(
                alignment: Alignment.bottomRight,
                child: Container(
                  margin: EdgeInsets.all(20),
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      border: Border.all(width: 2, color: Colors.white)),
                  child: Icon(
                    Icons.cancel,
                    color: Colors.white,
                  ),
                )),
          ),
          GestureDetector(
            onTap: () async {
              try {
                toastFunction(context, 'Image Downloading In Background');
                var imageId = await ImageDownloader.downloadImage(
                  widget.imgPath,
                  destination: AndroidDestinationType.custom()
                    ..subDirectory("Hd walls/image.jpeg"),
                );
              } catch (e) {
                print(e);
              }
            },
            child: new Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  margin: EdgeInsets.all(20),
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      border: Border.all(width: 2, color: Colors.white)),
                  child: Icon(
                    (_progresss != 100) ? Icons.arrow_downward : Icons.done,
                    color: Colors.white,
                  ),
                )),
          ),
          new Align(
            alignment: Alignment.bottomLeft,
            child: GestureDetector(
              onTap: () {
                setWallpaperDialog();

                ///_getBatteryLevel();
              },
              child: Container(
                margin: EdgeInsets.all(20),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(100),
                    border: Border.all(width: 2, color: Colors.white)),
                child: Icon(
                  Icons.image,
                  color: Colors.white,
                ),
              ),
            ),
          )
        ],
      ),
    ),
  ),
);
         }

              //function for the setting wallpaper in android;
             void setWallpaperDialog() {
           showDialog(
              context: context,
              builder: (context) {
    return Dialog(
      backgroundColor: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text(
              'Set a wallpaper',
              style: TextStyle(
                fontSize: 20.0,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
          ),
          ListTile(
            title: Text(
              'Home Screen',
              style: TextStyle(color: Colors.black),
            ),
            leading: Icon(
              Icons.home,
              color: Colors.black,
            ),
            onTap: () => _setWallpaper(1),
          ),
          ListTile(
            title: Text(
              'Lock Screen',
              style: TextStyle(color: Colors.black),
            ),
            leading: Icon(
              Icons.lock,
              color: Colors.black,
            ),
            onTap: () => _setWallpaper(2),
          ),
          ListTile(
            title: Text(
              'Both',
              style: TextStyle(color: Colors.black),
            ),
            leading: Icon(
              Icons.phone_android,
              color: Colors.black,
            ),
            onTap: () => _setWallpaper(3),
          ),
        ],
      ),
    );
  },
);
     }

    Future<void> _setWallpaper(int wallpaperType) async {
    var file =
    await DefaultCacheManager().getSingleFile(widget.imgPath);
     try {
         final int result = await platform
            .invokeMethod(
            'setWallpaper',[file.path,wallpaperType]);
              print('Wallpaper Updated.... $result');
           } on PlatformException catch (e) {
             print("Failed to Set Wallpaper: '${e.message}'.");
          }
        Fluttertoast.showToast(
         msg: "Wallpaper set successfully",
         toastLength: Toast.LENGTH_SHORT,
         gravity: ToastGravity.CENTER,
         timeInSecForIos: 1,
         backgroundColor: Colors.white,
         textColor: Colors.black,
         fontSize: 16.0);
         Navigator.pop(context);
         }
         Future<void> _getBatteryLevel() async {
         String batteryLevel;
          try {
          final int result = await platform.invokeMethod(
           'getBatteryLevel', {"path": "fuck bo"});
           batteryLevel = 'Battery level at $result % .';
           } on PlatformException catch (e) {
           batteryLevel = "Failed to get battery level: '${e.message}'.";
          }

           setState(() {
          _batteryLevel = batteryLevel;
          print(
                  '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
                      <<<<<<<<<<<<<<>$_batteryLevel');
          });
         }
         }

0 个答案:

没有答案