您的位置:首页 > 其它

安卓8.0以上的点击桌面插件打电话,判断点击控件

2018-03-01 12:37 309 查看
NewAppWidget.java

package com.linyb.widget_click_call;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
private static final String CLICK_ACTION="callChangePic";

public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
ComponentName watchWidget = new ComponentName(context,NewAppWidget.class);
views.setOnClickPendingIntent(R.id.btn,getPendingSelfIntent(context,CLICK_ACTION));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(watchWidget, views);
}

//小部件被添加时或者每次小部件更新时都会调用一次该方法,,每
// 个周期小部件都会自动更新一次,不是点击的时候更新,
// 而是到指定配置文件时间的时候才更新
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}

protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (CLICK_ACTION.equals(intent.getAction())) {

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

RemoteViews remoteViews;
ComponentName watchWidget;

remoteViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
watchWidget = new ComponentName(context, NewAppWidget.class);

remoteViews.setTextViewText(R.id.appwidget_text, "已经点击");
remoteViews.setImageViewResource(R.id.image_view, R.mipmap.ic_launcher_round);

appWidgetManager.updateAppWidget(watchWidget, remoteViews);

Intent call_intent=new Intent(Intent.ACTION_DIAL, Uri.parse("tel:112" ));
call_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(call_intent);

}
}
}


new_app_widget.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">

<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:background="#09C"
android:contentDescription="@string/appwidget_text"
android:layout_gravity="center"
android:text="@string/appwidget_text"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="bold|italic" />

<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="left|bottom"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#224433"
android:layout_gravity="right|top" />

</FrameLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.linyb.widget_click_call">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: