- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以基本上我只是 Android 的新手,我正在制作一个应用程序,它发送实时通知,该通知是从服务器用 php 发送的。
现在我希望当用户点击通知时在我的 webview 中打开一个网页。它是一个基于网络的应用程序。
我使用 3 个选项卡在 3 个 fragment 之间切换。每个 fragment 都有不同的 WebView 。
现在这是我的 GCMintentService.java:
package blah.blah;
import static blah.blah.CommonUtilities.SENDER_ID;
import static blah.blah.CommonUtilities.displayMessage;
import blah.blah.R;
import blah.blah.R.drawable;
import blah.blah.R.string;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
}
/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Toestel geregistreerd: regId = " + registrationId);
displayMessage(context, "Je toestel is geregistreerd");
Log.d("NAME", NotificationMain.name);
ServerUtilities.register(context, NotificationMain.name, NotificationMain.klas, registrationId);
}
/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Toestel nog niet geregistreerd!");
displayMessage(context, getString(R.string.gcm_unregistered));
ServerUtilities.unregister(context, registrationId);
}
/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = getString(R.string.gcm_deleted, total);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(context, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MyFragment.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
主要 Activity 包含 3 个具有所有不同 web View 的 fragment 。我从一个名为 FragmentAdapter.java 的数组中获取 url,它扩展了 FragmentAdapter。
工作完美......在这里是 fragment 适配器:
package blah.blah;
import blah.blah.MyFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
String[] toVisit={
"www.google.com",
"www.stackoverlow.com",
"www.lorumipsum.com",
};
final int PAGE_COUNT = 3;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
return MyFragment.newInstance(toVisit[position]);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0)
{
return "Klassen";
}
else if(position == 1)
{
return "Docenten";
}
else
{
return "Lokalen";
}
}
}
但是如何根据从服务器检索到的信息加载不同的 url? ,GCM 推送通过带有通常代码的 php 发送。
应该是 intent.PushExtra("google.com, http://google.com ")//例如一切都不起作用。我希望我的问题很清楚。
感谢所有帮助!只需分享您的所有提示和想法即可:P。
哦,还有我的 MyFragment.java,如果它有用的话:
package blah.blah;
import blah.blah.R;
import blah.blah.R.id;
import blah.blah.R.layout;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MyFragment extends Fragment{
WebView browser;
String url;
private Bundle webViewBundle;
Intent extras = getActivity().getIntent();
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(
R.layout.myfragment_layout,
container,
false);
final ProgressBar spinner = (ProgressBar)view.findViewById(R.id.progress);
browser=(WebView)view.findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
browser.loadUrl("file:///android_asset/geeninternet.html");
}
@SuppressWarnings("unused")
public void onProgressChanged(final WebView view, final int progress)
{
if(progress == 100)
spinner.setVisibility(View.GONE);
}
});
browser.loadUrl(url);
// Just load whatever URL this fragment is
// created with.
return view;
}
// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(String url)
{
MyFragment f=new MyFragment();
f.url=url;
return f;
}
// Met browser;
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
browser.loadUrl(url);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Sla webview op
*/
@Override
public void onPause()
{
super.onPause();
webViewBundle = new Bundle();
browser.saveState(webViewBundle);
}
/**
* Herstel staat van webview
*/
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (webViewBundle != null)
{
browser.restoreState(webViewBundle);
}
}
}
最佳答案
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Ontvangen bericht");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
在此代码中,您仅从通知负载中获取一个值 - price
键的值。
您没有包含服务器代码,所以我不知道您是否在发送带有通知的 URL。如果这样做,您应该从 onMessage
方法中的 intent
获取它的值,并将其传递给打开 web View 的代码。如果不这样做,则应更改服务器代码以将 URL 包含在通知负载中。
编辑:
为了将 URL 传递给您的 fragment :
从 onMessage()
中的 intent 的 extras 中获取 URL。
将其传递给 generateNotification
。
将 URL 作为额外添加到将在点击通知时启动 Activity 的 Intent :notificationIntent.putExtra ("url",url)
。
在 fragment 中,在您初始化 WebView 的代码中,从启动包含该 fragment 的 Activity 的 Intent 的额外部分获取 URL (getActivity().getIntent()。 getExtras().getString("url"
).
编辑2:
您应该在 onActivityCreated()
中加载 WebView ,而不是在 onCreateView()
中。 onCreateView()
在 Activity 创建之前被调用,因此 getActivity()
将返回 null。
不要按原样使用该代码:String test = (getActivity().getIntent().getExtras().getString("url"));
确保 getActivity()
不返回 null。我相信当 getActivity()
不为 null 时,您可以假设 getIntent()
和 getExtras()
不会返回 null,但检查起来更安全。 getString("url")
如果 Activity 未从通知中打开,则返回 null。因此,您应该确保它在加载 WebView 之前具有值。
通知标志可能有问题:
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
如果新 Activity 已经存在(例如,如果应用程序已经在运行),通知似乎不会打开新 Activity 。在这种情况下,将不会调用 onCreateView()
和 onActivityCreated()
的 fragment 代码,也不会加载 URL。您应该更改标志以便始终创建新 Activity ,或者您应该将作为通知结果加载 WebView 的逻辑放在其他地方 - 可能在 onResume()
中。
关于java - 从通知获取 url 并加载到 Fragment 的 webview 中的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16596993/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!